Determining parent page of template - templates

Inside a Grails GSP template (e.g., _form.gsp), how can I determine what page is calling the template (e.g., create.gsp or edit.gsp) so that different fields can be displayed?

There is no general way, no. If you just know that in your application that the foo action always renders the foo view and there aren't exceptions to that, then of course you can just look at the action name. You could also do something simple like put something in the model when rendering the template, like <g:render template="foo" model="[parentPage:'edit']"/>, or set a variable in pageScope or similar. There are ways to accomplish what you want, but no general way in a template to answer the question "What GSP rendered this template?". I hope that helps.

So I googled it without any mention of templates (gsp determine current page) and found the answer on this Nabble thread. I will use params.action to determine what action I'm currently doing (e.g., create or edit), and use that to display different fields.
Cheers!

Related

Handlebars partial vs. render vs. template

The Fire Up Ember.js screencast uses partial, template, render to render templates within templates, but I'm still not sure I know which one to use when.
In the same screencast the partial is explained as using the context and all data relative to the current controller, while render uses the the specified controller's matching template, context etc:
Can someone please clarify the differences between partial, template, render and when (examples) to use which?
The way I understand it, the way they break down is like this:
"render" gives you an entire view/controller/template context of its own to work with.
An example will be a top navigation that includes dynamic pieces. The content will be maintained within a TopNavController and inserted into the application template using "render"
"partial" will insert a template, but that template will be using the current context instead of its own. Partial is also a newer part of the framework, meant to replace using template to some extent.
An example would be showing a list of users and having each user be a relatively complicated piece in the list (avatar, name, email, etc) you can just loop through the list and insert the partial based in the context of each user.
"template" just inserts the template using the current context. I believe it's not good style though to use template to render pieces inside of a template, you should rather use "partial" although template will work the same way for most cases.
This chart given in ember's website gives a good comparison between render, partial and view.
Here is a snippet image of the comparison given in the website:
In terms of client side memory usage which one is better, render or partial.
Assuming partial would be better as few lesser objects

Using template language in Django in a DRY way

I've troubles using Django template language in a DRY way.
I defined base-section.html, which is a base for all sections of my website.
Each section must override some pieces of code in this basic template to provide proper title, icon, description, body of the section, etc - always in same way.
Now, I want to write some code in each section, but I don't like the idea of overriding about 10 blocks in each section, because it would be writing same things again and again.
Whereas keyword for specified section is X:
title of the section is X, too
title of the website in a browser is X, too
name of icon is X+"_icon.png"
and so on
I'll appreciate a solution where I can assign only 1 or 2 variables per section and have my templates working.
Is it possible?
A lot of this data could simply be passed along with the view. However, I often find that writing a custom template tag might be preferable.
You could take a look at custom tags in the Django documentation: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/
Using either a simple tag or an inclusion tag, you can quite easily reuse template code.

Combining multiple Django Views/Templates

I am working on a website. On the Homepage, I want to show the posts in the center and show famous tags on the right side of the page. Now, posts and tags are two different apps and their views/tempaltes are calculated/generated in two separate functions.
How can I show what I want on my Homepage keeping my mind that its two different views being called. As far as I understand, I can only call a single view to show my homepage.
This must be possible but I am unable to understand this. Any help will be much appreciated.
I think you need to create a custom template tag that in this case called: Inclusion tags
You can look into writing a custom template tag for the tags. That way the tag logic can stay in the right application.
It depends how these different apps render the templates. If they just render the template without extending from a base template you could simply use AJAX to load all the content into your homepage.
If not, then there is no other way than writing a custom solution for this. This could be an extension to your views, a custom template tag as already mentioned or something else according to what exactly you need.

Multiple views loaded into a template

Hay guys, in PHP when i want to load a differnet page (render, with all variables outputting) i would use
include 'mypage.php'
This would load the page up and render the output.
How do i do something like this in Django? My problem is that i have a "quick list" which lists a bunch of popular items. I want this list to appear on every page, even if i don't call the object.
any ideas?
EDIT: The view is called shop.app.popular_items
You can use Inclusion tags.
Use template context_processors
To answer your question to becomingGuru on when to use context processors and when to use inclusion tags, remember that context processors are run on every template render. So they should only be used when you know you will always need the value.
Edit:
After Daniel's answer and as you said you want to have it on every page,
becomingGuru's answer is probably the best solution.
This does not belong into the view (if you not want to display this as a standalone page).
This should go into a custom template tag.

How can I pass a standard, static variable from all views in Django?

I'm working on a blog application, and I want to have a sidebar that includes a list of all the months the blog has been in existence, to provide links to archives pages. Moreover, I'd like to make this automatically update when the month changes, rather than hardcoding it in the template. Of course, as far as I can tell, this means that I'll have to calculate the list of months in every view, and pass it into every template from every view.
I'd like to avoid this, if possible. Is there a way to calculate the list once and automatically apply it to every template, without having to explicitly pass it into the template from every view?
There are a few possible solutions to your problem.
If you really want to have this on every page on your site a context processor is probably your best choice. Context processors are basic way to inject data into all template contexts. Be aware however that the context processor will be called on every request.
An alternative solution would be to create a custom template tag and use it on a shared base template for all of the pages you wish to have your sidebar. Template tags are a bit more complex to create but they are more flexible.
With either solution you should also look at Django's cache framework. The cache framework makes it pretty easy to temporarily store your calculated values for a while to save some work on each request.
You want a template context processor
Django - having middleware communicate with views/templates
http://docs.djangoproject.com/en/dev/ref/templates/api/?from=olddocs#id1
Django's template inheritance should cover this. You could create a base template that handles your sidebar functionality. Your other views extend this template.
Template Inheritance:
http://www.djangobook.com/en/1.0/chapter04/#s-template-inheritance
A combination of custom template tags as mentioned previously and template fragment caching should do the trick.