Are there such things as partials in Symfony 2, reusable templates from anywhere, effectively?
I have found include http://twig.sensiolabs.org/doc/tags/include.html but this only allows the rendering of a template in a specific directory structure.
What I really want a folder that contains all my partial templates, rather than lumping them into my main views?
So I want to be able to do something like
{% include "Bundle:Default:Partials:view.html.twig" %}
Update
I do not want to use the enforced structure of Bundle:Controller:Template structure. I do not want to use this as it means putting all my template partials in with my main view templates. I need something that lets me do Bundle:Controller:PartialDir:Template
You can already do that. The symfony2 docs has a section describing how to do this.
http://symfony.com/doc/current/book/templating.html#including-other-templates
In Symfony 2.4 (the version I am currently using but it probably works in other 2.x versions as well) you can do the following:
{% include '::_partials/partial.html.twig' %}
or in Symfony 2.2+ using the include function
{{ include('::_partials/partial.html.twig') }}
This will look for the partial.html.twig template inside of the app/Resources/views/_partials directory. You can obviously name the _partials directory whatever you want. It also works without the '::' prefix.
I know this is old, but the way to achieve what OP asks is the following:
Rather than doing
bundle:controler:partialDir:template
we have to switch it slightly to achieve:
{% include 'Bundle:PartialDir/Controller:Template' %}
To include the controller, you'll need to refer to it using the standard string syntax for controllers (i.e. bundle:controller:action):
{{ render(controller(
'App\\Controller\\ArticleController::recentArticles',
{ 'max': 3 }
)) }}
Related
in flask templates, we combine HTML pages using extends, but my view(HTML) files are in diff folder. so how u can extend that? how to set a path? eg: {% extends 'NavigationEG/layout.html' %} NavigationEG is my folder but this way not work
without a folder, it works but I want to store it in one folder.
Like bergp said in this related question on stackoverflow, flask is looking for templates in the given templates folder. If you want to reference a different folder in your template, you could remove the template_folder when initlializing the app and refering to the full path in the render_template parameters.
Depending on the reason for the different folders, blueprints can have their own template_folder as well.
I hope this helps. If not, please share your file structure and parts of the code so we can help you find the source of this problem. Good luck.
I'm using Flask-Security in my project and I would like to extend templates (namely login) it offers, in order to style them with custom CSS. Following the documentation, I've changed the SECURITY_LOGIN_USER_TEMPLATE config value to point to my template. In this template I'm extending the template provided by Flask-Security ("security/login_user.html") and changing the content of some of the blocks it defines (or its base).
My template renders as expected (looks like the standard "security/login_user.html"), the only problem being is that the blocks I'm trying to overwrite are not overwritten.
My template looks like this: login.html:
{% extends "security/login_user.html" %}
{% block content %}
<p>There should be no form.</p>
{% endblock content %}
To confirm that I'm not not rendering the original template I've tried changing contents of the template file to empty and it works as expected - nothing is rendered.
What am I missing?
After some time with debugger, I realized that something is wrong with the package Flask-Security that is available on PyPi. When developing my project I relied on source code that is present on their official github page. If you take a look at the login template, you notice that it indeed extends base template which provides definition of the blocks you can later overwrite.
However when you download the package from PyPi and then check the template you notice that it differs - it does not use Jinja2 inheritance, nor does it use any blocks, therefore the problem described in the question.
In other words, PyPi package is reflecting latest stable release (as noted by nick-k9), which differs significantly from develop branch.
I am building a site in Django CMS. In my templates directory for the project is base.html.
I am writing an app "was_this_helpful" to add a dialog box on some pages for users to give feedback. I want to include a file from was_this_helpful/templates into base.html but it says the file does not exist.
{% include 'was_this_helpful/dialog.html' %}
My file structure look like this:
- was_this_helpful
- templates
- was_this_helpful
- dialog2.html
- dialog.html
- required app files
I read somewhere that sometimes template files need to be another level deeper in templates to be found which is why I made the dialog2.html but still it's not working. I do not understand how to accomplish this. Based on what I've read it should work. Is it different because I'm not in another app, just the templates directory?
Without knowing more it's hard to tell if it's a simple solution or not.
The way you have your code written, there is not a was_this_helpful/dialog.html - you only have a dialog2.html inside your was_this_helpful so was_this_helpful/dialog2.html would be the reference path.
I've always created another folder inside my templates folder with the name of the directory above my templates folder. Just like you have with your was_this_helpful second directory. I find that this makes it much easier to extend base.html files.
You can always do it absolutely too by two periods before the path call, so ../was_this_helpful/templates/dialog.html
If you don't have luck with that either, there is an {% extends %} method as well which might accomplish what you're trying to do as well.
Good luck!
I'm trying to introduce some tools at work to improve code quality. An obvious solution that I'd used at a previous company was to run jslint before checking in code.
The problem is that we are using Django to do our templating (though, I assume we would have a similar problem with other templating languages).
How is it possible to take code like the below, and have it JSLint/JSHint properly while ignoring the template tags?
var a = { "test" : "test"};
{% comment %}
{% endcomment %}
{{ my_variable }}
window.x = "y";
I've seen this question specifically regarding JSHint, which looks like it could handle some cases, but it doesn't address inlining variables, like {{ my_variable }} above.
Is there any way to ignore certain lines using JSHint/JSLint, or otherwise get the linting to execute correctly?
Much like linting coffeescript with these tools, you're trying to lint content outside it's pervue. Probably easiest to run the template with dummy values, and JSHint that resulting file.
Probably duplicate of How to run JSHint on files with Django template markup in them?
I am attempting to override/extend the header for the Django admin in version 1.2.1. However when I try to extend the admin template and simply change what I need documented here: http://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-vs-replacing-an-admin-template), I run into a recursion problem.
I have an index.html file in my project's templates/admin/ directory that starts with
{% extends "admin/index.html" %}
But it seems that this is referencing the local index file (a.k.a. itself) rather than the default Django copy. I want to extend the default Django template and simply change a few blocks. When I try this file, I get a recursion depth error.
How can I extend parts of the admin? Thanks.
SOLUTION: Rather than extending, I copied the files into my_templates_directory/admin/ and just edited them as I wished. This solution was acceptable, though not ideal.
The contrib/admin/templates/admin path needs to go before the directory with your custom admin templates in your list of paths in TEMPLATE_DIRS in your settings.py
Create a symlink to contrib/admin/templates/admin/ in your templates directory and use it in your {% extends %} statement.
cd /path/to/project/templates/
ln -s /path/to/django/contrib/admin/templates/admin/ django_admin
Now in your admin/index.html use {% extends "django_admin/index.html" %}
EDIT: Just realized that you're on windows... Not sure how to achieve the same results. Hopefully this still helps the folks on linux.
SOLUTION: Rather than extending, I copied the files into my_templates_directory/admin/ and just edited them as I wished. This solution was acceptable, though not ideal.