pug-php / pug-symfony

Pug (Jade) template engine for Symfony

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Global template variables not defined

qsollet opened this issue · comments

First, thank you for this very handy bundle.

Testing pug template with symfony we've found that the app variable is not defined when rendering (http://symfony.com/doc/current/book/templating.html#global-template-variables).

The error appends there:
each flash_message in app.session.flashbag.get('error')

And here is the complied version:
<?php foreach (\Jade\Compiler::getPropertyFromAnything(\Jade\Compiler::getPropertyFromAnything(\Jade\Compiler::getPropertyFromAnything($app, 'session'), 'flashbag'), 'get')('error') as $flash_message) { ?>

Do you know how we could globally define it? And if it's possible to have something like this also http://symfony.com/doc/current/cookbook/templating/global_variables.html ?

Thank you for your help

commented

I started to think about a way to do it. It's not yet possible.

For the moment, you should:

  • Pass $app from the controller to the view each time you need it.
  • Or use some ugly workaround like:
    In a filter, service or an event listener
$GLOBALS['app'] = $app;

And in your template:

- global $app
each flash_message in app.session.flashbag.get('error')

Thank you for the answer.

We'll stick with twig for now to avoid anything ugly workaround. We've check in Symfony and it doesn't look like there is any easy way to get $app.

We've also started to use assetic for assets but this may be hard to implement with you bundle. Do you think it will be hard to implement? Do you have any recommendation on where to start if we wanted to do this implementation?

commented

Hi, we do not plan to port or implement assetic in pug-symfony for compatibility reasons but we will soon release a pug plugin to handle assets (dev/prod mode, concat, minification, less, stylus, coffee, react and any third-party you wish). Pug-symfony will embed this plug-in.

commented

All you ask should now be possible, tell me if you can now use:

each flash_message in app.session.flashbag.get('error')

You should also now use the asset() function:

link(rel="icon" type="image/x-icon" href=asset('favicon.ico'))

And to manage your assets, you can usr pug-minify:
https://github.com/pug-php/pug-minify

This plug-in is included and preconfigured for Symfony by pug-symfony. So you just have to put your assets in Resources/assets/js, Resources/assets/css in your bundle or in your app directory, then you can call them with:

minify-to output-filename
  link(rel="stylesheet" href="css/mystylesheet.css")
  script(src="js/myscript.js")

They will be compiled in the web directory automatically.

To get the new version just enter composer update. Please let me know your thinking about this new stuff.

Hello,

Thanks for the great work! We'll test as soon as we can and provide feedback, we should be able to do it by the end of the week.

Cheers

We managed to do some test. The "asset" function is working well.
However when we're trying to call the function "path" or "csrf_token" we have an error 500.

It is for a login page:

form(action=path('login_check') method="post")
    input(type="hidden" name="_csrf_token" value=csrf_token('authenticate'))

Let me know if you need any debug log.

commented

As I said, I only implemented asset() and add app as a global variable. There are many functions in Twig and no easy way to port them all at once as far as I know.

But there are only shorcut, you can use the original methods, this should be equivalent:

form(action=view.router.getPath('login_check') method="post")
    input(type="hidden" name="_csrf_token" value=view.form.csrfToken('authenticate'))

You can find all the functions in the symfony sources at Symfony\Bridge\Twig\Extension. I will have to add them and check them one by one.

Let me know if you have any trouble with asset or app.

During all the test we did we had no issue with asset or app. We can access the value we need.

For the minify-to the route is not found, we're still looking into it because it can be an error on our side of the code.

Thanks!

commented

Added and tested:

  • asset
  • asset_version
  • csrf_token
  • random
  • url
  • path
  • absolute_url
  • relative_path
  • is_granted

Also added but not tested with real firewall:

  • logout_url
  • logout_path

I don't I will can add many other functions. There are too much specific.

I close this issue but if you have some feedback, do not hesitate to add comments or open a new issue if you have trouble with the new functions.

We've tested most of them and could not find any issue.
Thanks a lot for your work, we'll test this on a new project and let you know if we find any bugs.

Cheers,