I want to be able to add a css class to a field in django 1.8 and jinja2.
Previously I was doing something like this:
{{ form.country|add_class('form-control')|safe }}
That was using django-widget-tweaks previously, but now that django 1.8 has native jinja support, what is the best way to do this?
Related
I've been looking into how to use i18n for translations in an Angular app. My struggle is with string interpolation, it seems this has not been implemented into Angular yet.
Goal: Use i18n to translate string interpolation of collection in an Angular app to have a Spanish version.
What I've done: I've been doing a lot of research, and the workarounds I've seen mentioned are:
Polyfill: ( https://github.com/ngx-translate/i18n-polyfill ) (and referenced here: angular-i18n Angular 6 Internationalisation : How to deal with variables )
Hidden div: (Angular internationalization (i18n) and *ngFor )
Verbose method: using n-container and ngswitch. Seems the least clean way: https://github.com/angular/angular/issues/11405#issuecomment-415731284
For a collection like an array, or a database table, how are these workarounds creating separate i18n tags for each element? Will it end up showing just one tag for all elements of the collection?
For example: If you have an array for "Gender", with values of "Male" and "Female", what will happen?
<mat-option *ngFor="let gender of genders" [value]="gender.value">
{{ gender.viewVal }}
</mat-option>
In the polyfill they mention:
test {{myVar}} !", {myVar: "^_^"})
Does that mean "^_^" is the translation? It's done directly in the typescript, not in an xlf file?
If you use a hidden div, how does using the typescript file affect the xlf? The verbose method seems easiest to understand but not good for scalable, what if you have a dropdown with 10+ items, then that really bulks up the code.
What is the best way to go about this goal? I'm not sure I properly understand how the polyfill works and there is not much documentation on these ways of translation.
I have been able to add syntax highlighting to my .liquid files by following the instructions here: Enabling Liquid templating syntax highlight in webStorm/phpStorm
It worked fine for my HTML and liquid syntax highlighting as it's so similar to Twig.
But my issue is I also have my schema included in each of my .liquid templates. The schema is JSON but there's not syntax highlighting on it at all.
Is there a way to add a custom syntax highlighting for a file type if it's wrapped in some sort of delimiter?
My schema is wrapped like so:
{% schema %}
JSON object with my settings/configuration
{% endschema %}
See image below:
As #yole have said: you cannot do that. Well... permanently.
You can always inject JSON there manually .. and it will last for a while (a session for sure).
Just place caret just after {% schema %} and hit Alt + Enter.
Choose Inject language or reference and locate JSON in the list (speed search works there as well, so just start typing).
Result is obvious:
You are using Twig plugin for .liquid files (native support for them (RUBY-7210) does not seem to be on JetBrains short list right now).
It's now possible to have permanent Language Injection in custom Twig tag (using Twig plugin). See the screenshot below for custom injection rule that you can create yourself:
You can inject JSON in your {% schema %} block manually via Alt+Enter, Inject language or reference > JSON:
See also https://blog.jetbrains.com/phpstorm/2017/12/twig-handling-improvements/
At my org we use a custom .amg filetype for an internal framework that is essentially just a JSON
I made IntelliJ treat those as JSONs by adding *.amg pattern under Recognized File Types > JSON
reference: JetBrains / File type associations
I have just started with Pelican. It's awesome, I just can't figure out how to use macros in my articles (and pages). I know I can use Jinja when making my own theme, but I can't seem to be able to use it in articles. I'd like to be able to define a macro/function/template/whatever that I'd put directly in the markdown of the article, possibly with parameters, and it would get expanded when the pages are generated. For example a function to generate an image with a caption of given size and css class that would also be a link directly to the image. I'd like to be able to access these macros from all articles, to be able to reuse them everywhere. Something I'd normally do with PhP.
I could probably use JS to do this, but if I'd like to avoid it and keep everything static if possible. Can this be done?
UPDATE:
I found a pelican plugin that solves this - jinja2content.
OLD SOLUTION:
I found a solution here. You can implement a filter in Python to process all texts in articles/pages like this:
Create a python file filters.py in which you write the filter function process_text to expand my macros (or generally do anything with the article/page text), for example to test the function write something like:
def process_text(input_text):
return "TEST " + input_text
In the Pelican config file (pelicanconfig.py) register this function as a possible filter to be used with Jinja:
import sys
sys.path.append('.')
import filters
JINJA_FILTERS = {'process_text':filters.process_text}
Now you have to edit the templates to apply this filter to article/page texts before adding them to output. In my case I edited two files: themes/themename/templates/article.html and themes/themename/templates/post.html and changed {{ article.content }} to {{ article.content|process_text }} and {{ page.content }} to {{ page.content|process_text }} in them to apply the filter.
Now all texts in articles and pages should be prefixed with "TEST".
The not so convenient thing about this is I have to write my own macro expander, which shouldn't be extremely hard with regular expression in Python, but if there is a nicer way to do this, feel free to post here.
Ive built the all the views (html, css, jquery) for a web app and now im starting work on the back end, using Laravel / Mysql.
I recently came across scaffolding in Laravel, similar to Ruby on Rails, which is great, but the forms ive built for my views all contain special classes and markup is there a way to specify the classes and other markup to be applied to form when using scaffolding ?
If I got you correctly, you can set an extra variable on controller, or check URI or route on views, and specify extra classes by looking to the view.
E.g: In the controller, you can do something like this:
return View::make('views.myview')->with('type','form');
And in the view, you can use simple if clause(s):
#if(isset($type))
<div class="{{$type}}">
#else
<div class="default">
#endif
For adding extra markup and assets to views, I use teeplus/asset, which is ported from Laravel 3 to 4 (old habits die hard :)).
packagist URL
documentation
I am working on a django project (my first), and in one of my views, I have a sophisticated html snippet with JS weaved within it. I would like to reuse this "component" somewhere else in the same view. Is there a way of achieving this? Please let me know if this design is faulty to begin with?
Use the {% include '/my/common/template.html' %} templatetag.
Loads a template and renders it with
the current context. This is a way of
"including" other templates within a
template.
The template name can either be a
variable or a hard-coded (quoted)
string, in either single or double
quotes.
I know it's an old one but maybe someone is gonna have use of this answer.
There's also the inclusion tag. It's like the include tag, only you can pass it arguments and process it as a seperate template.
Put this in my_app/templatetags/my_templatetags.py:
#register.inclusion_tag('my_snippet.html')
def my_snippet(url, title):
return {'url': url, 'title': title}
and then my_snippet.html can be:
{{ title }}
then, to use this snippet in your templates:
{% load my_templatetags %}
{% my_snippet "/homepage/" "Homepage" %}
More info:
https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#howto-custom-template-tags-inclusion-tags
Not sure, if you like to reuse your HTML in different templates (rendered by different views). If so, look into Django's template inheritance mechanism:
The most powerful -- and thus the most complex -- part of Django's template engine is template inheritance. Template inheritance allows you to build a base "skeleton" template that contains all the common elements of your site and defines blocks that child templates can override.
You should try Django custom template tags. This way you will keep your snippets in an external file and then call them easily by something like {{ your_custom_tag }}. It's a very convenient method for working with reusable chunks of xhtml markup. You can even use arguments with these custom tags, something like {{ your_custom_tag|image:"logo.png" }}.
You can learn more about custom tags here.