Meaning of the "#" symbol in Twig - symfony

I dont understand what does # do in this routing?
exemple:
{% extends "#User/Default/index.html.twig" %}
i am trying to find the file that is extended but unable to do so.

Thanks to the comment left by #Cerad I found it using: bin/console debug:twig
#User src\UserBundle/Resources/views\

Usually, when you refer to a template, you'll use the MyBundle:User:index.html.twig format.
Twig also natively offers a feature called "namespaced paths", and support is built-in automatically for all of your bundles.
Take the following paths as an example:
{% extends "MyBundle:User:layout.html.twig" %}
With namespaced paths, the following works as well:
{% extends "#App/layout.html.twig" %}
Both paths are valid and functional by default in Symfony.

Related

Include external PHP application into iFrame (Twig/Symfony)

I am working on a proof-of-concept project that needs to be built quickly, so we are taking shortcuts. I have created the basic Symfony/Twig structure. A colleague has created a PHP application that we want to include into an iFrame on the site (eventually we will do this by means of proper templates, etc.).
I created the following index.html.twig (simplified):
{% extends 'base.html.twig' %}
{% block body %}
<iframe src="project/index.php"></iframe>
{% endblock %}
This throws me an error:
Unable to find template "project/index.php"
I understand the error, but am not sure what to do so that the external application simply gets loaded into the iFrame. Basically I want Symfony and Twig to ignore it (for now, so it can be used as prove of concept).
I will make an assumption here, specifically, that your code is included in a template for /app/somethingelse (two slashes or more).
which will lead to, the uri in the following (your code)
<iframe src="project/index.php"></iframe>
to be interpreted as /app/project/index.php - since it's a relative path -, which most likely doesn't exist. What you probably want is an absolute path (which always begins with a slash):
{% block body %}
<iframe src="/project/index.php"></iframe>
{% endblock %}{# ^here #}
I suggest reading up on relative vs. absolute paths. combined with the comment, that you almost always want absolute paths (unless full URLs are necessary of course) - this also includes paths to images, paths to css files, paths to anything and everything really. relative paths can be quite handy, but you always have to be aware that the relative position of locations gets relevant and cannot be changed without impacting other places where those relative positions are assumed to be static.

What's the difference between the include tag and the include function?

I saw someone including a template on his application using the twig template system.
He included his template like this:
{{ include( 'Bundlename:Directory:template.html.twig' ) }}
instead of this:
{% include 'Bundlename:Directory:template.html.twig' %}
At first I thought he was using a custom twig extension, but it actually
works on symfony in general, even though its not documented.
http://twig.sensiolabs.org/doc/tags/include.html
So is there a difference between the two?
why is it not documented?
if the syntax is wrong why is it even working?
It is documented http://twig.sensiolabs.org/doc/functions/include.html.
Twig tag include vs function include

Twig absolute path variable in include

my script defines a absolute path in PHP and gives it to the twig template with the render()-function. All works well, I can access my variables with {{ varName }} in the view. But if I try to use a absolute path as a variable inside a include-command like {% include varName %} it will say: Unable to find template. This will even happen if the absolute path is correct.
What am I missing here?
Well it's weird but unfortunately I had no possibility to include a file from somewhere else not in the specific symfony structure. So I had to move all contents to the view folder.
I hope someone fixes this, which is anything but flexible.
To add template search paths to Twig_Loader_Filesystem, you can use
$loader->addPath($templateDir);
or
$loader->prependPath($templateDir);
Where $templateDir is a parent directory and $loader is the variable containing your Twig_Loader_Filesystem object. In your example, you would want $templateDir to be the path where the Bundle directory is located.
Then, in your include do something like this:
{% include 'Bundle\Somedir\somefile.html.twig' %}
Twig will then search all paths in looking for Bundle\Somedir\somefile.html.twig and will return the first one it finds.
The Twig documentation on this subject can be found here: http://twig.sensiolabs.org/doc/api.html#twig-loader-filesystem
Note: Choice of using addPath or prependPath depends on which order you'd like Twig to search the directories in. Paths added using prependPath will be searched before any of the previously loaded paths.
Caution: The search order applies to all templates so if the path/filename you use in the include statement is not unique, you could end up overriding another template.

Symfony special characters in Twig file causing error

For pages with lots of text and html tags, I quickly get a parsing error from Twig due to unexpected special characters. Has anybody else ran into this problem?
Unexpected character "!" in ... at line 76
I am trying to fix this problem by pasting the static information in a new page and then including it using include.
My question is, where should I place a static HTML file so it can be included in a Twig file using {% include 'content.html' %}? Currently the included file is in the FooBundle/Resources/views/Default directory.
What is a good practice for overcoming this problem, and if include is the answer, where should the file be located to be included in a twig?
Note:
When I try to link to it using {% include 'content.html' %} I get the following Symfony error:
Unable to find template "content.html" in ... at line 31 (broken link)
I found a solution, drawing from various different posts. I ended up putting the raw html in a new file (which isn't required) and included it as follows:
{% include 'FooBundle:Default:content.html.twig' %}
Then inside my content.html.twig I surrounded my raw HTML code with raw Twig tags:
{% raw %}
// RAW HTML CODE HERE
{% endraw %}
Include like this
{% include 'YourProjectNameFooBundle:Default/content.html.twig' %}

Message: You have requested a non-existent service "http"

I have a problem with render tag in twig.
I got a message: "You have requested a non-existent service "http". When i use {% render url("render_menu") %}
render_menu route works fine
help me !
P/S: Sorry about my english
You use a correct way, but with an old version. You should upgrade to fix security issues AND resolve your problem.
A security issue changed the way to use render: http://symfony.com/blog/security-release-symfony-2-0-20-and-2-1-5-released
Documentation has been updated:
http://symfony.com/doc/2.0/book/http_cache.html#using-edge-side-includes
You are using render tag wrong. If you want to render action, that is behind your render_menu - you should pass it in format YourBundle:YourController:YourAction.
For example, if you have menuAction() that is behind route render_menu, then in Twig you should call it like this:
{% render "YourBundle:YourController:menu" %}
Note, that you have to strip out Action word when calling menuAction in render tag.

Resources