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.
Related
I just build a blog for myself, build up by hexo and next.
Today i have known how to set one picture by this :
![image description](img's url)
But by this way i cant put two images on same line.It loos like this:
image1
image2
And this is what i want
image1 image2
I have checked disable fancybox and tried to use this (just write them at the same line):
![image description](img's url)![image description](img's url)
They are both no effect.
I didn't notice that it's the solution of NEXT theme,so I cannot guarantee the answer is still valid.
Original answer:
I also tried to do that before,but failed many times.
Eventually I got the answer in Official Reference Document.
To be brief,use the format below to insert your imgs.
{% gp [number]-[layout] %}
![image description](img's url)
![image description](img's url)
{% endgp %}
[number] is the amount of your imgs.
[layout] is the index of the designed layout. (you can see specific
layouts in the Document)
P.S. The Document recommends enabling the "fancybox" plugin,which I did.
So I am not sure whether it would operate normally without enabling fancybox.Please notice!
For example,you can get:
"image1 image2" (you mentioned in your question)
by using the code below:
{% gp 2-2 %}
![image description](img's url)
![image description](img's url)
{% endgp %}
Hope my answer would be helpful! I kown it's really a struggle to grope alone. :)))
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.
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' %}
Have the following css section:
.introduction:before {
content: 'Who is going to translate me in a graceful way?';
}
Probably there is no easy way to handle i18n for css file, two possible choices:
gather all these css which with content and ship them together within Django templates.
generate i18n sensitive css name in template to choose different pre-translated css content.
Any new possible solutions will be appreciated, thanks.
I would serve the css file that needs translating through Django and cache it per language to mitigate the performance hit.
urls.py
url(r'^static/css/translated.css$', TemplateView.as_view(template_name='translated.css')),
translated.css
{% load cache %}
{% cache 60*60*24 translated_css LANGUAGE_CODE %}
{# write css here, translate as a normal template #}
.introduction:before { content:
{% trans 'Who is going to translate me in a graceful way?' %}
; }
{% endcache %}
I think that this is impossible. Only you can translate it if the css is a template (like the html template). But this is a bad solution.... efficiency
But if you have a class language in the body for example, you can have something like this:
body.language_en .introduction:before { content: 'Who is going to translate me in a graceful way?'; }
body.language_es .introduction:before { content: 'El traductor de google es terrible :-)'; }
And in your base.html something like this:
....
<body class="language_{{ LANGUAGE_CODE }}">
....
Another possible solution, for people that want to avoid serving the CSS files differently or duplicating CSS lines or files, would be to make use of django javascript translation support.
Leave the original CSS in place as a backup. Then override the content using javascript by appending the translated style to the head of the page:
var trans_str = gettext("some content");
$('<style>.classWithContent:before{content:"' + trans_str + '" !important}' +
'</style>').appendTo('head');
This is the version using JQuery but you can rewrite it in pure javascript easily. Add as many translated strings and override as many styles as you want.
This way you make use of django internationalization which you probably already have in place and translate CSS strings with minimal code. This solutions is obviously not applicable for heavy use of CSS content.
We have a single webinterface in Symfony2 which is used by a number of
clients.
Each client enters our site by a different URL.
For example:
client A enters from clientA.our-domain.com
client B enters from clientB.our-domain.com
client C might even join from clientC.com, i.e. using their own selected domain instead of a sub-domain of ours
However, regardless of the URL they use, they end up at the same
machine: our website.
Right now we use the same CSS for all of them, regardless of which
client. We would love it if we could design a stylesheet for
each client with client-specific color schemes.
I don't want to set up a separate webserver for each client, so I'm
looking to select the stylesheet dynamically.
Now, in the HTTP 'Host' header one can see which URL the client used,
right.
Can this be used, for example in app.php, to set some global variable
which defines the client?
Twig might read this variable in the main template and decide which
stylesheet to use based on it, perhaps?
Also, should we then use css variables? This might be preferable to keeping complete stylesheet 'clones' for each client, if only the colors change.
So to sum up:
Can the 'host' HTTP header be used in Symfony2 to indicate to Twig which stylesheet to use?
Should we use CSS variables to identify the colors in the scheme, or is there a better way?
Thanks in advance!
Dieter
Personally, I'd prefer using a special CSS file for each site (containing the styling, that's different for each site, like colors or background images). E.g. site-clientA.our-domain.com.css.
Then you could use something like this:
<link rel="stylesheet" type="text/css"
href="/path/to/css/site-{{ app.request.host }}.css" />
Or
{% if app.request.host in ['site-clientA.our-domain.com', 'site-clientB.our-domain.com', 'site-clientC.our-domain.com'] %}
<link rel="stylesheet" type="text/css"
href="/path/to/css/site-style1.css" />
{% elseif ... %}
...
{% endif %}