Can recipient variables be used to conditionally render sections - mailgun

I am trying to render sections of a template depending on recipient variables.
I am able to control sections if I reference the normal mailgun variables.
Given the following template
{{name}}
{{#A}}
A - {{name}}
{{/A}}
{{#B}}
B - {{name}}
{{/B}}
To make this template render A or B, I pass A and B in
h:X-Mailgun-Variables='{"A":"true" ,"name": "My Name"}'
Is it possible for the template to render A or B from recipient variables in the case of batch sending, something like this where recipient variables are
h:X-Mailgun-Recipient-Variables='{"a#email.com":{ "A":"true", "a":"A value"},
"b#email.com":{ "B":"true", "b":"B value"}}
and the template is something like this
{{#%recipient.A%}}
A - %recipient.a%
{{/%recipient.A%}}
{{#%recipient.B%}}
B - %recipient.b%
{{/%recipient.B%}}
Is something like this possible ?

I guess you might have used the The if block helper described here.

Related

How to get current url or input parameters in web.py template

I have a web page designed with web.py. I use web.template.render() and an HTML template file to draw a table in this web page.
I pass a list of items to this template, and would like to show the data in different pages using GET method and offset and count parameters (i.e. http://mywebsite/records?offset=10&count=15).
I am looking for a way to get the input values of offset and count and also the current url in the HTML template file, so I would be able to put links to next page and previous page.
Templates get data either passed into them (e.g., render.home(a, b, c)) or as a global (explicitly passed in as a dict via globals parameter when you define your renderer).
Your example is easier to do by getting the information in python, and passing it into the template. This allows you to error check the values, provide defaults, etc.
So, have your GET() extract the proper information and pass it to the template:
class index(object):
def GET(self):
my_render.index(url=web.ctx.fullpath,
offset=web.input().offset,
count=web.input().count())
==== index.html ====
$def with(url, offset, count)
<table>
<tr><td>URL is: $url</td>
<td>OFFSET is: $offset</td>
<td>COUNT is: $count</td></tr>
</table>

How to render a component based on model parameter

I have the requirement to render a template based on a property of my model.
My stratergy was to use custom if block helpers for the handle bars templates, something like this:
{{#ifequals type 'cars'}}
{{cars-component}}
{{/ifequals}}
{{#ifequals type 'planes'}}
{{planes-component}}
{{/ifequals}
but I cant create a block helper that resolves parameter passed by the template to the helper.
if I use Handlebars.registerHandlebars, it resolves the variable name insted of the variable.
the reason i need to do this is because it is part of a plugin framework.
Option blocks (special if statements) don't work properly in ember handlebars helpers. https://github.com/emberjs/ember.js/issues/2237
isCar: Ember.computed.equals('type', 'cars')
isPlane: Ember.computed.equals('type', 'planes')
{{#if isCar}}
{{cars-component}}
{{/if}}
{{#if isPlan}}
{{planes-component}}
{{/if}}
It almost looks like you can do it, but there is a problem with it, as is pointed out in the github issue above.
Ember.Handlebars.helper('iff', function(value,property, options) {
if(value === property){
return options.fn.apply();
} else {
return options.inverse.apply();
}
});
http://emberjs.jsbin.com/UhOWeWiJ/1/edit
http://emberjs.jsbin.com/UhOWeWiJ/2/edit
You will get a bound blocks error when you try and implement something like this and the values actually change
https://github.com/emberjs/ember.js/issues/2237
Uncaught You can't use appendChild outside of the rendering process
http://emberjs.jsbin.com/UhOWeWiJ/3/edit
http://emberjs.jsbin.com/UhOWeWiJ/3/edit

Django's equivalence of ASP.NET UserControl

If anyone here is ASP.NET pro, you might know what I mean by user control. I wish to create a similar one in django instead.
So, my problem is that I have several pages in my website, but I need a search bar to appear in every pages. Since I require the views.py to operate this search bar, I cannot do a simple method of
{% include 'something.html' %}
Therefore, can anyone suggest how can I do it?
There are a couple of ways to accomplish what you're wanting to do:
Context Processors
Template Tags
Context Processors can augment the template context with values, regardless of which template is loaded. They are akin to filters in Rails.
Template Tags, like Context Processors, can accomplish anything you can do in Python, but are implemented at the template level.
If you need something to be present on every template, one of the simplest ways to accomplish this is with an inclusion tag, which can also accept values passed to it. An inclusion tag could be implemented at your highest level template, a.k.a your MasterPage, and as long as you don't put it in a block and override it, it would appear on every page that includes that template in its inheritance chain.
If it's just something you want to include on every page, and it doesn't need to do any processing, you should just be able to place the code you want in the top-most template and have subsequent templates inherit that.
I typically have a "base.html" template that all of my templates inherit from. If I need something to be in every page, I put it there. If it's something I want there by default, but want to be able to augment it in subsequent templates, I will place it into a block. That block will let you include or override its default content.
I know this post is kind of old but I just came across it and found a kind-of-solution that works. I call it kind-of-solution because it is a workaround.
I have a few different sites on which I want to display logging information. This display always looks the same (it has the same html) and has the same database table and model class behind it.
My solution/workaround uses the django filters:
in views.py I put the list of log-entries in the context
context = {'list_log': Log.objects.filter(condition = True) }
template = loader.get_template('my_html_file.html')
return HttpResponse(template.render(context, request))
in my_html_file.html I use a custom filter
{{ list_log|get_log_uc|safe }}
in the filters.py I load another html file with this custom filter
#register.filter
def get_log_uc(list_log):
template = loader.get_template('user_control_log.html')
context = { 'list_log' : log }
return template.render(context)
in user_control_log.html I have the user control equivalent html
{% for log in list_log %}
<p>log.something</p>
{% endfor %

Django template retrospection

Is there an available method to take a django template and see which objects it needs in the context to render without errors? Alternatively, is it possible to find out programatically, after rendering a template, if any bad template variables were present?
My goal is as follows. Imagine I have a template:
Hello {{ name }}.
Your lucky number is {{lucky_number}}
I would either like a list containing 'name' and 'lucky_number'. Alternatively, if I render that template and provide a context with just 'name', I would like to know that 'lucky_number' was not provided.
I found this which can help me build my alternate solution:
http://excess.org/article/2012/04/paranoid-django-templates/

Django tags and filters in Lift?

This is a generalization of my previous question about pluralize filter:
Does lift have an equivalent of Django's tags and filters?
Tags are small piece of predefined code that can be used directly in html template, for example:
{% now "jS F Y H:i" %}
renders the time right now in the given format.
Filters
Filters operate (in html template) on the context variables in the template, for example:
{{ value|capfirst }}
if called on a value "john" will result in "John". Or:
{{ value|length }}
will render the length of the string into the template.
As you can see the filters operate on the context variables that are passed to the template.
Considering tags, you could define those yourself with snippets.
As snippet is basically a callback much as a Django tag is. You don’t get any easier syntax, though, because Lift’s templates are pure XML/Html.
<Lift:Tag.now format="jS F Y H:i" />
And the logic would be defined in
class Tag {
def now: NodeSeq = // ...
}
Filtering is something you generally can’t do in a Lift template because Lift doesn’t allow any variables in a template. The whole concept is thus inapplicable. (You could do XML transforms or or bind magic but that would be a bit too much for a simple value.length.)
No, if you need the length of some value in your Html, you will have to define that inside the snippet and expose it.
If you really can’t live without filters in your template (though I can assure you, it is a good thing to separate all HTML template and code and it works once you are used to it), have a look at Scalate which may be used together with Lift as well.
This kind of logic should be in the render method of a snippet. Display code in pure Scala (rather than in a template language hybrid) is a first-class citizen with respect to testing, IDE's and refactoring tools.
These kinds of transforms don't come built-in, but you can add them with implicits:
class HappyString(s: String) {
def capfirst = ....
}
implicit def toHappyString(s: String) = new HappyString(s)
Then call these in your render method before binding whatever value it is you're generating.