Symfony 2 and Twig from MySql - symfony

I am trying to create a basic CMS in Symfony2. Is it possible to "translate" twig code from mysql to controller somehow?
For example let say in mysql there is
{% include 'FOO::FOO.html.twig' %}
When I get it from orm how to make it load the template instead of just echo
{% include 'FOO::FOO.html.twig' %}
Thanks a lot.

You can use the internal Lexer, parser and compiler of twig to get a php object representation of your templates:
http://twig.sensiolabs.org/doc/internals.html

Related

Symfony generate css

I need a little help.
Imagine that in database for every user have stored color of background.
Everytime when user login, first for that user in some folder is generated css file with name of id of user and included in html template.
I need help to understand how to generate css file ?
Thanks.
I Symfony with FOSUserBundle, you can use app.user to get the user in your twig files.
If you open a MySQL console and enter describe fos_user; then you will see all the fields you can use in your Twig file. So in your table you could store for each of the FOS users a color
Then in Twig you could use something like this:
{% if app.user %}
{% set backColor = app.user.color %}
{% else %}
{% set backColor = 'none' %}
{% endif %}
...
<body style="background-color:{{ backColor }};">
You get the idea...
You might want to figure out a way to store the color preference for the FOS user using a Symfony form. And then store preference in database. You can raise a separate question for that if you have difficulties - it should be easy.
Quite simple...
You need to generate a twig file for every user in your desired folder, and place "inline" CSS in it.
{% block css %}
<style>
.class {
parameter: {{ entity.value }};
}
</style>
{% endblock css %}
To generate the file, create a service, which you will call when a page is loaded.
It will have to first check if the file exist, if it does, it will read from it (no query to the DB), if not, it will generate the file, and read need data from DB.
Look at answer from me and Gopal on this page to know how to make a service How to use session in Symfony.
You sould be able to use it for your need... ;)
Also look at the Symfony Filesystem Component
file_put_contents() should help you write content of the file... ;)

Typehint (intellisense) objects within a twig loop

I've searched for a solution but I can't find any suitable for Twig, I know it's a quality of life thing. But it would make templating slightly easier.
Example
{% for userObj in userObjCollection %}
{{ userObj.property }}
{# I want to type hint userObj so I can use intellisense to get the right property #}
{% endfor %}
I'm currently using the PHPStorm IDE,
any suggestions or pointers are greatly appreciated.
Edit
Note that this is pure a question for an easier way to code within the loop. Not something a user of a site would come in contact with. (Also added phpstorm tag to clarify)
Edit 2
I've got the Symfony2 and PHP Annotations plugins installed (Sorry for not pointing this out earlier)
Use this in your twig:
{# #var foo \FooObject #}
Personally I use the Symfony Support plugin for PhpStorm, it has auto-complete for twig objects and even repositories.

NetBeans use custom code templates for a TWIG file

In my twig view files for a Symfony application I need to write a log {% trans %}Foo Bar Baz{% endtrans %} what is quite annoying.
Therefore I tried to make a template, where I just have to write trans press space and the magic is done.
I made a new Template in Twig File, Twig Block and Twig Variable but non of them worked. The Code I used was:
{% trans %}${TEXT}{% endtrans %}
Abbreviation is trans
Then I restarted NetBeans, but unfortunately nothing happens when I write trans in a .twig file.
What am I doing wrong? How do I have to do this?
Thanks (:
You should add your Code Template in the Twig Block language, not Twig File language.
You don't need to reboot netbeans.
Then, type relatively quickly: {% trenter
You'll come up with:
If you don't want to type {% at all, you can choose HTML language instead of Twig Block language (assuming your file name ends with .html.twig).

FOSUserBundle - overriding templates

I've just installed FOSUserBundle and I'm trying to override the templates to use my own custom layout.
I have a bundle, BMCmsBundle, that has a layout.html.twig file in
src/BM/CmsBundle/Resources/views/layout.html.twig
Looking through the docs, I have created the following file:
app/Resources/FOSUserBundle/views/layout.html.twig
How do I then use my own template, i.e. CSS/jS?
I've tried in the FOS layout.html.twig file to use the following:
{% extends 'BMCmsBundle::layout.html.twig' %}
But once again, this doesn't seem to work, any ideas why?
Thanks
After creating your new layout at
app/Resources/FOSUserBundle/views/layout.html.twig
Make sure you also clear your cache:
php app/console cache:clear
It is the opposite.
Your layout in BMCmsBundle has to extend the one in app/Resources with the command :
{% extends "::layout.html.twig" %}

Get current URI in Twig template

Does anybody know how to get the current URI in a Twig template?
I’ve read through the documentation and I’m unable to find the Twig function to do this.
{{ app.request.uri }}
If you want to read it into a view variable:
{% set uri = app.request.uri %}
The app global view variable contains all sorts of useful shortcuts, such as app.session and app.security.token.user, that reference the services you might use in a controller.

Resources