Unable to find template Symfony2 - symfony

I would like to include a template in my view but it doesn't work, I have this error :
Unable to find template "::StyleBlock/light-pattern.html.twig" in ::base.html.twig at line 46.
My code :
{% for zone in content.blocks %}
{% set path = '::StyleBlock/' ~ zone.styles %}
{% include path %}
{% endfor %}
In the details i have this message :
InvalidArgumentException: The file "views/StyleBlock/light-pattern.html.twig" does not exist (in: /var/www/gathena/app/Resources).
But the path is correct, i don't understand.
I use Symfony 2.3 and I have the good permission on my directory

You have given wrong path, it should be:
{% for zone in content.blocks %}
{% set path = 'CmsCmsBundle:StyleBlock:' ~ zone.styles %}
{% include path %}
{% endfor %}
as for path src/Cms/CmsBundle/Resources/views/StyleBlock/
The first parameter is your bundle, second is the controller in this case StyleBlock, so your views are in your bundle in Resources/views/StyleBlock directory, last parameter is the template name which is defined by your loop variable in this case. It should only be your template name, without any absolute paths. All parameters are seperated by :

Try this :
::StyleBlock:light-pattern.html.twig

Related

Symfony-twig template form theme error

I have everything working correctly and now I'm trying to work with form themes. This is my code to generate the form without a theme.
{% extends 'base.html.twig' %}
{% block body %}
{% include 'menu/menu.html.twig' %}
{% if addpost is defined %}
<div id='add_post_form'>
{{ form_start(addpost) }}
{{ form_widget(addpost) }}
{{ form_end(addpost) }}
</div>
{% endif %}
{% endblock %}
But when I'm adding a form-theme with the following code
{% form_theme form 'form/form_div_layout.html.twig' %}
I get this error:
Variable "form" does not exist
When i execute this without the line, I'm getting the following error:
Unknown "form_help" function. Did you mean "form_rest", "form_end"?
the form_div_layout.html.twig contains the code found at github symfony twig form theme
At my config.yml I've added the following under the twig section,
form_themes:
- 'form/form_div_layout.html.twig'
.
either not, i still have this error
what is missing ???
My file structure
If all your forms are going to use the same theme you only need to add the line in your config, but if you want a particular form theme in a particular template you can use the template tag.
The reason you're getting the 'form is not defined error' is because you don't have a variable called form passed the the template, your form variable is called addpost, so you need to use
{% form_theme addpost 'form/form_div_layout.html.twig' %}

Symfony2.7 Twig How to obtain a token?

Inside Twig file I have this code:
{% set player = app.security.getToken().getUser().getPlayer() %}
{% if player.getSelectedCharacter() is not null %}
{% set character = player.getSelectedCharacter() %}
{% .... %}
{% endif %}
But at now, app.security is deprecated. So I want to change this. I can obtain user token inside my controller and send it to the Twig. But I prefer to get it directly via Twig.
How I can do this?
As you said and mentioned in the documentation.
The app.security global is deprecated as of 2.6. The user is already
available as app.user and is_granted() is registered as function.
I think you can just try something like this in your view.
app.user.getPlayer()

Symfony 2 setting up translation files

I am failing to setup translation for my Symfony 2 project. I have manually created a folder inside app\Resources\translations\message.en.yml and it's content:
base:
title:
homePage: TeamERP IMS for BA
Then on the base twig template inside my bundle I am trying to call it:
<title>
{% block title %}
{{ base.title.homePage|trans }}
{% endblock %}
</title>
Then on the config.yml I has the following:
framework:
translator: { fallbacks: en }
I am getting this error:
Variable "base" does not exist in TeamERPBaseBundle::base.html.twig at line 7
What am I doing wrong?
Edit: after fixing the problem here:
{{ 'base.title.homePage'|trans }}
I stopped getting the error, thanks for that. now the page is not giveng the error but not loading the page with the warning in the logs fine:
[2015-05-01 12:42:57] translation.WARNING: Translation not found. {"id":"base.title.homePage","domain":"messages","locale":"en"} []
[2015-05-01 12:42:57] translation.WARNING: Translation not found. {"id":"Home","domain":"messages","locale":"en"} []
Edit2: There was some kind of problem with my version of symfony 2.6. I just did a composer update due to this, and it started working. normaly.
First of all it should be messages.en.yml as indicated by #xurshid29, but most important it should be
<title>
{% block title %}
{{ 'base.title.homePage'|trans }}
{% endblock %}
</title>
inside the template. The value passed to the trans filter must be a string but base.title.homePage|trans would be expanded to something similar to $base->getTitle()->getHomepage() because it's a Twig variable syntax. That's why you're getting the error message Variable "base" does not exist.
Rename message.en.yml to messages.en.yml, it should work.
The format of your yaml file is imho not correct and the translation in twig is wrong.
message.en.yml
base.title.homePage: TeamERP IMS for BA
your.twig.html
<title>
{% block title %}
{{ 'base.title.homePage' | trans }}
{% endblock %}
</title>
You can use the Translation component as you like, but better is to write down correct sentences in your main language (TeamERP IMS for BA) and translate them. Think of giving a translation file away to native speaker who should translate it:
message.de.yml
TeamERP IMS for BA: TeamERP IMS für BA
your.twig.html
<title>
{% block title %}
{% trans %}TeamERP IMS for BA{% endtrans %}
{% endblock %}
</title>
And of course check the domains {% trans_default_domain "message" %}

save sonata media path into twig variable

I want to store full path of image (sonata media bundle) into variable in twig. Is that possible?
If I write:
{% set pic = path item.image, 'big' %}
it throws me an error: Unexpected token "name" of value "item" ("end of statement block" expected) ...
If I write:
{% set pic = item.image %}
then it works, but it stores only name of the file, not full path.
Why don't you do like this ?
{% set rendered %}{% path item.image, 'big' %}{% endset %}
....
Here is my path {{ rendered }}
There is not such a function available (there is a path() function to generate routes). You have to create your own twig extension with this custom function. Read all about that in the documentation.
Perhaps you can solve with:
<img src="{% path media, 'small' %}" data-href="{% path media, 'big' %}">

Use Twig Custom Set Variables From an Include

I'm trying to include a twig file with a bunch of custom set variables and then use the variables in the multiple other template files. Similar to how including a PHP file works.
I don't seem to have access to the variables set inside the include in my index file.
Is there any way to do this?
Sample Code *Edited
Included File:
{# variables.html #}
{% set width = "100" %}
{% set height = "250" %}
Template File:
{# index.html #}
{% include 'variables.html' %}
{{ width }}
{{ height }}
Expected Outcome:
100 250
Actual Outcome:
// Nothing gets output
I was just trying to do the same thing you were and came up with the following:
Created snippets.twig to maintain all these mini variables. In your case, you might call it variables.twig. In this file, I used a macro without any arguments. I was creating formatted entry date markup that I can use across all my templates and it looked like this:
{% macro entry_date() %}
<time datetime="{{post.post_date|date('m-d-Y')}}">{{post.post_date|date('F j, Y')}}</time>
{% endmacro %}
note that the parenthesis after the name declaration were imperative
In my main layout file, layout.twig, I referenced this macro via an import statement so it would be accessible in all child templates:
{% import "snippets.twig" as snippets %}
<!doctype html>
...
In my template files, I now have snippets accessible and can query it like any other variable:
{{ snippets.entry_date }}
UPDATE
This doesn't seem to correctly run code. If you're just storing static content, you should be good. You can also pass args to the macro so I imagine you could make some magic happen there but I haven't tried it.
As far as I know it is only possible with {% extends %} tag. Instead of including template with variables you should extend it.
Example:
variables.tpl:
{% set some_variable='123' %}
... more variables ...
{% block content %}
{% endblock %}
template.tpl
{% extends 'variables.tpl' %}
{% block content %}
{{ some_variable }}
... more code which uses variables assigned in variables.tpl ...
{% endblock %}
You can use a template extension : https://symfony.com/doc/current/templating/twig_extension.html
post|entry_date

Resources