clojure luminus global variable access into template - clojure

I'm trying to do a simple thing(at least for common frameworks) using Luminus and Selmer template but I can’t find a way to do it. I would like just to pass values to the base template, something like this:
file: home.html
{% extends "base.html" {:user {{ user }} } %}
​{% block content %}
...
​{% endblock %}
I know that we can do it using render a file, like this
(render-file "html/base.html" {:user user})
But it not looks right, it looks like a weird workaround.
My idea actually is to make some global variables to access anywhere of application like user session, but I didn’t find it in luminus documentation

Well, even when Selmer is cool, most of the web culture in Clojure turns around Hiccup as the template generator. You will use Hiccup with ClojureScript (Reagent - Re-frame) and that is another reason to switch to it. Probably you already have Hiccup installed, check it with:
lein deps :tree | grep hiccup
You will need to create a layout like this one. And uses it in a "controller" like this to build your HTML view.

Related

Trouble extending Flask-Security login template

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.

What's the best way to DRY in Angular within Django?

I'm new to Angular, and to Django, and to DRY (Don't Repeat Yourself) so forgive me if this really simple and obvious... I've somehow missed it.
I have a dozen or so partials and a couple of them share a similar code block. I've learned how to use {% include 'template.html' %} to keep things DRY in django templates, however this doesn't work inside client side partials.
So I looked into ng-include with a <div ng-include="/groups/template.html"></div> and while that kind of works it changes the scope (which is over my head) so I cannot get my partials code to work on that included codeblock. But my biggest issue here is that the code block I'm including has to be via a routable URL in the urls.py file. This seems to go against security and usability.
Is what I'm asking for even possible? I'd love Angular to grab an external codeblock and drop it into the partials file server side when it puts the partials file into the cache.
Your Angular templates should be placed in your static folder like js files.
The structure can be like this one:
static/
js/
views/ #Put here your angular templates
Then, you can call ng-include like this:
<div ng-include="{{ STATIC_URL }}views/template.html"></div>

Template Partials in Symfony 2

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 }
)) }}

How to run JSLint / JSHint in a file with templates?

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?

Cannot Extend Django 1.2.1 Admin Template

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.