Template Namespacing - templates

I have this line on one of my Service Providers:
\View::addNamespace('foo', asset('/bar'));
But doesn't render on my Blade template using the following line:
{{ HTML::image("foo::img.".$image, "Image") }}
Instead of rendering the above line, it displays a literal "foo::img.animagesomwwhereinmysite.png". Is there a way I can make this possible?

View::addNamespace only tells your Service Provider of the particular location where you are storing views; that declaration itself doesn't pass anything along to the view. (See the docs.)
So when you are calling the helper function {{ HTML::image() }}, it is expecting, as its first parameter, the URL of the image. In your case, you are specifying the URL as the literal string "foo::img.imagename".
Assuming your images are in the directory foo/img/, this should work:
{{ HTML::image("foo/img/".$image, "Image") }}

Related

Django get the last part of the path in the URL in the template

I have a path https:///something.com/asset/asset_app/location_laptop/POA/
I want to exract "POA" part and write in my template {{ request.##### }}
how would i do that?
URL patterns:
path("asset_app/location_laptop/export-excel/<str:location>/", views.LocationLaptopListExcelView.as_view(), name="asset_app_location_laptop_list-excel"),
The POA is the location URL parameter. You can access this with:
{{ view.kwargs.location }}
The view object is always passed to the template, and the named URL parameters are stored as .kwargs attribute. We can obtain kwargs['location'] with .location in a Django template variable.

String manipulation in Django templates

Imagine the context variable {{ url }} outputs www.example.com/<uuid-string> where <uuid-string> is different every time, whereas the former part of the URL stays the same.
Can one change the output of {{ url }} to instead www.forexample.com/<uuid-string>, via solely manipulating the string in the template and without involving views.py (which I know is the better way to do it, but that's not the question).
An illustrative example would be great.
read about filters and templatetags - they are a methods that allows you to perform some actions on variables in templates.
You can also create your own tags and filters that allow you to perform action non-built into Django template language
Simple example of such filter:
#in templatetags.py
#register.filter(name='duplicate')
def duplicate(value):
return value*2
#in your template
<p> {{ url|duplicate }} </p>
You can find more examples here. Also there you will find tutorial how to use and create them

Drupal 8 Twig Template: get relative or absolute path from URI

I have in a twig template an image url, like "public://path/to/image.png". I want to get "sites/default/files/path/to/image.png" to use it in the template.
I thought it should work with something like this:
{{ url ('public://path/to/image.png') }}
or
{{ file_uri_scheme ('public://path/to/image.png') }}
But all I get are errors. I know that the second one is a PHP function, but is there any chance to do it with TWIG?
You can use the twig function file_url to get a relative path:
{{ file_url('public://path/to/image.png') }}
Checkout the documentation under https://www.drupal.org/node/2486991
It accepts a uri and creates a relative path to the file. At least in Drupal 8.1.
Retrieving a URL to a page from a "path" is deprecated in D8. https://www.drupal.org/node/2073811
EDIT:
nah im wrong and just missunderstood the spelling ;/
the correct answer is: they are working on a twig extension.
i was searching for a way to get my image uri / urls and achieved it with
{{ node.field_image[delta].entity.url }}

How to render template into subdirectory with handlebars/ember

Say I have a handlebars file stored in some_directory/some_template.handlebars
I want to render this template using render command so that I can hook it up to child controller.
I've tried:
{{ render "someDirectory/someTemplate" post }}
{{ render "some_directory/some_template" post }}
none of these works. If I move some_template.handlebars to root directory, then it works when I do:
{{ render "someTemplate" }}
but I would like to avoid this since I'm finding that the root directory is getting cluttered. It's worth mentioning I use the ember-rails gem.
how are you packaging up the templates? are you sure it's even including the template in the browser?
You can find all the template names (keys) on Em.TEMPLATES object (see console in example below)
http://emberjs.jsbin.com/ejolaWOQ/1/edit
Ok,
I figured something out that works, you want to do:
<!-- note, did this in emblem, not sure if it translates to handlebars -->
{{ render 'nameOfController' someOptionalModel templateName='someDirectory/someTemplate' }}

Template Contexts not recognized from external file (Django)

So, I've been editing a website and have many JavaScript functions that utilize the Contexts that the views.py file passes to the page. Until now, these functions have been contained in the base.html file and so have been loaded onto every page directly. However, to make things cleaner, I copy and pasted all the functions to an external .js file. Now, rather than use the contexts, the functions consider them to be literal strings.
Example:
$('#title').text('{{ event.name }}');
The above line will actually set the text of the element to say "{{ event.name }}" rather than the name of the event. Any ideas on how to fix this? I really don't want to keep these functions in the base file where they can be seen by anyone who inspects the page source.
It doesn't matter if you put your javascript functions in an external file or in your base.html it would still get exposed to the user. Only a minification of the file would actually help to trick the user from seeing the actual values but with javascript all your code is public.
Why you're having this problem is because when you rendered the javascript inline (in your base.html) you had access to the template context.
This is no longer the case and the Django template engine doesn't interpolate your {{ event.name }} anymore.
The problem you're facing as well is a good one. You should never mix and match javascript with Djangos template language or any template language for that matter and the only way of fixing it is to
a) start pulling the values from the DOM ie. render a proper DOM
b) to start to fetch the values from the server, traditionally using AJAX.
And the smallest example that I can muster at the moment is below:
Your view:
def my_django_view(request):
return HttpResponse(json.dumps({'meaningoflife':42}), mimetype='application/json')
Your HTML
<input type="hidden" id="myMeaning" value="{{ meaningoflife }}" />
Your javascript
var meaning = document.querySelector('#myMeaning').value;
alert(meaning); //should alert 42.
In your view you return some form of render_to_response which takes a template argument and a context argument. What the render_to_response function does is read your template, and replace all {{ placeholders }} with the values passed via the context dictionary.
Templates are essentially a complex version of this
"""
<h1>{{ person.name }}</h1>
<p>{{ person.phone_number }}</p>
""".format(person)
The problem is the templating engine does not know files specified by a scripts src attribute is actually a Django template. To fix this don't use the script src attribute. Instead do something like this.
<!--base.html-->
<h1>Site Title</h1>
<p>Some content</p>
<script>
{% include 'jsfile.js' %}
</script>
Using the include statement should do the trick.