I have a simple test in an Azure API Manager Outgoing Policy
<policies>
<inbound>
<base />
</inbound>
<backend>
<base />
</backend>
<outbound>
<set-body template="liquid">
{% if context.Request.OriginalUrl.Query.param1 == 'test' %}
Matched
{% else %}
Not Matched
{% endif %}
Hello : {{context.Request.OriginalUrl.Query.param1}}
</set-body>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
And I post
/echo/resource?param1=test
I get
Not Matched
Hello : test
I can't figure out the syntax to check the value for param1 and act accordingly in the body. I've not found any decent documentation on this which helps. I've tried this as well as
{% if context.Request.OriginalUrl.Query.param1.Equals('test') %}
Can somebody advise on the syntax I need to check this? This should be trivial and it's driving me nuts! :)
Thanks
It works for me if I use a variable:
<policies>
<inbound>
<set-variable name="param" value="#(context.Request.Url.Query.GetValueOrDefault("param1"))" />
<base />
</inbound>
<backend>
<base />
</backend>
<outbound>
<set-body template="liquid">
{% if context.Variables["param"] == 'test' %}
Matched
{% else %}
Not Matched
{% endif %}
Hello : {{context.Request.OriginalUrl.Query.param1}}
</set-body>
<base />
</outbound>
<on-error>
<base />
</on-error>
Result:
Matched
Hello: test
In addition to Markus's answer you can also do it like this
{% if context.Request.OriginalUrl.Query.param1[0] == 'test' %}
It makes sense that it's an array of values and that's why the comparison was failing.
Related
In an APIM policy for a REST API, I'm trying to create a SOAP request for the backend service.
The URI of the REST request is /GetCustomer/{accountNumber}.
In the Liquid if statement below, I'm trying to check if the MatchedParameters collection contains the account number. This doesn't work, I get the value from the else branch.
I tried a couple of variations:
{% if {{context.Request.MatchedParameters.ContainsKey("accountNumber")}} %}
{% if #(context.Request.MatchedParameters.ContainsKey("accountNumber")) %}
{% if context.Request.MatchedParameters.ContainsKey("accountNumber") %}
<set-body template="liquid">
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://my-services.com/CustomerService/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<GetCustomer>
<request>
{% if {{context.Request.MatchedParameters.ContainsKey("accountNumber")}} %}
<AccountNumber>{{context.Request.MatchedParameters["accountNumber"]}}</AccountNumber>
{% else %}
<AccountNumber xsi:nil="true" />
{% endif %}
</request>
</GetCustomer>
</soap:Body>
</soap:Envelope>
</set-body>
Leaving the if statement out works correctly, then the request is constructed as I expect:
<set-body template="liquid">
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://my-services.com/CustomerService/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<GetCustomer>
<request>
<AccountNumber>{{context.Request.MatchedParameters["accountNumber"]}}</AccountNumber>
</request>
</GetCustomer>
</soap:Body>
</soap:Envelope>
</set-body>
So the question is: is it possible to access the context from within a Liquid if statement? And if so, what is the correct syntax?
I test it in my side, you just need to write the liquid like:
{% if context.Request.MatchedParameters["accountNumber"] != null %}
{% else %}
{% endif %}
And by the way: According to my test, if you specify a {accountNumber} in the request url of APIM. We have to requsest it with ...../GetCustomer/{accountNumber}, if we request it with ...../GetCustomer, it will show 404 error. So you do not need to worry about "if context.Request.MatchedParameters["accountNumber"] is exist"
I currently have this part in my base.html Pelican template:
{% if article and article.author %}
<meta name="author" content="{{ article.author }}" />
<meta name="copyright" content="{{ article.author }}" />
{% elif page and page.author %}
<meta name="author" content="{{ page.author }}" />
<meta name="copyright" content="{{ page.author }}" />
{% else %}
<meta name="author" content="{{ AUTHOR }}" />
<meta name="copyright" content="{{ AUTHOR }}" />
{% endif %}
Is there a way to simplify this, e.g. something like
<meta name="author" content="{{ publication.author }}" />
<meta name="copyright" content="{{ publication.author }}" />
where publication is either page or article? (I guess it is never possible that something is both, a page and an article?)
You could do this:
{% if article.author %} {% set author = article.author %}
{% elif page.author %} {% set author = page.author %}
{% else %} {% set author = AUTHOR %} {% endif %}
{% if page or article %}
<meta name="author" content="{{ author }}" />
<meta name="copyright" content="{{ author }}" />
{% endif %}
You should be able to erase the last "if" because it's not necessary if you put this in blocks which belong to pages or articles.
I'm using jekyll-postfiles plugins to make my life easier. This plugin allows me to have static content in subfolders for my blog posts. For example:
_posts/
├── 2018-10-10-a.md
└── 2018-10-11-b/
├── 2018-10-11-b.md
└── b.png
And I can use the image as a locall file in the markdown: ![](b.png) in the 2018-10-11-b.md. This plugin makes all the magic of copying the file and making the links work.
But now I want to use jekyll-seo-tag and I want to set YAML variables like this:
---
image: "{{ page.url }}b.png"
---
This is just to create custom metadata in the HTML file, i don't use the variable in my blog post. But I can't make this work. The page.url liquid variable is not expanded and the final metadata looks like this:
<meta property="og:image" content="myBlog/%7B%7B%20page.url%20%7D%7D%2Fmap.png" />
instead of:
<meta property="og:image" content="myBlog/2018/10/11/b/b.png" />
This property is in the head of the html page. Some questions here in StackOverflow show how to get a variable from the front matter and parse the liquid markup in the body of the document. What I need is to parse the liquid markup before the processing of the markdown file.
Is it possible to make the YAML front matter parse the liquid variables before processing the file?
It is not clear to me why the front matter should contain a variable.
In most cases you do not need variables in the front matter. I think that this is also the case in your situation. I would move the front matter variable to the layout file. Thus, the layout file should look like this:
<meta property="og:image" content="myBlog/{{ page.url }}/{{ page.image }}" />
... and the front matter should look like this:
---
image: "b.png"
---
The SEO plugin can be easily exchanged by SEO without plugin, written by me. It is easy to adjust to your needs. Here is the code that it adds to the head:
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{% if page.title %}{{ page.title }} | {% endif %}{{ site.title }}</title>
{% assign pagecontent_description = page.content | markdownify | replace: '.', '. ' | replace: '</h2>', ': ' | replace: '</h3>', ': ' | replace: '</h4>', ': ' | strip_html | strip_newlines | replace: ' ', ' ' | truncate: 160 %}
<meta name="description" content="{% if pagecontent_description.size > 10 %}{{ pagecontent_description }}{% else %}{{ site.description }}{% endif %}">
<link rel="shortcut icon" type="image/png" href="/img/icon-196x196.png">
<link rel="shortcut icon" sizes="196x196" href="/img/icon-196x196.png">
<link rel="apple-touch-icon" href="/img/icon-196x196.png">
<!-- Facebook and Twitter integration -->
<meta property="og:title" content="{{ page.title }}"/>
{% if page.image %}<meta property="og:image" content="{{ page.image }}"/>{% endif %}
<meta property="og:url" content="{{ site.url }}{{ page.url }}"/>
<meta property="og:type" content="article">
<meta property="og:image" content="{{ site.url }}{{ page.image }}"/>
<meta property="og:site_name" content="{{ site.title }}"/>
<meta property="og:description" content="{% if pagecontent_description.size > 10 %}{{ pagecontent_description }}{% else %}{{ site.description }}{% endif %}"/>
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="#{{ site.twitter_url }}">
<meta name="twitter:title" content="{{ page.title }}" />
{% if page.image %}<meta name="twitter:image" content="{{ site.url }}{{ page.image }}" />{% endif %}
<meta name="twitter:url" content="{{ site.url }}{{ page.url }}" />
<meta name="twitter:description" content="{% if pagecontent_description.size > 10 %}{{ pagecontent_description }}{% else %}{{ site.description }}{% endif %}" />
<link rel="canonical" href="{{ page.url | replace:'index.html','' | prepend: site.baseurl | prepend: site.url }}">
<link rel="alternate" type="application/rss+xml" title="{{ site.title }}" href="{{ "/feed.xml" | prepend: site.baseurl | prepend: site.url }}">
<link rel="sitemap" type="application/xml" title="Sitemap" href="{{ "/sitemap.xml" | prepend: site.baseurl | prepend: site.url }}" />
If you have any questions, please let me know.
I have a fairly simple setup. I created a flask app and call index.html on route '/'.
index.html contains this:
{% extends "template.html" %}
{% block content %}
<h4 class="centeredText">
HEADER TEXT
</h4>
{% for p in paragraph %}
<p class="centeredText">
{{ p }}
</p>
{% endfor %}
{% endblock %}
My template.html is also very simple:
<html>
<link rel="stylesheet" media="screen" href = "{{ url_for('static', filename='bootstrap.min.css') }}">
<link rel="stylesheet" media="screen" href = "{{ url_for('static', filename='custom.css') }}">
<link rel="stylesheet" media="screen" href = "{{ url_for('static', filename='custom_navbar.css') }}">
<link href='http://fonts.googleapis.com/css?family=Josefin+Sans:300' rel='stylesheet' type='text/css'>
<!--<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css"> -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<head>
<h1>{% if pageType = 'home' %} HOME {% endif %}</h1>
</head>
<body> <h1>TEMPLATE</h1>
<div class="container">
{% block content %}
{% endblock %}
</div>
</body>
</html>
While testing I get this error:
expected token 'end of statement block', got '='
Edit: I missed including the variable in the header in template.html. Which I now know, to be the source of the error. Is there anyway to pass a variable to template.html, or is that not good practice?
Help!
The pageType variable should appear in your template.html so long as you pass it to the render_template function. In your case, you have a syntax error here:
{% if pageType = 'home' %}
You need to use == instead, so it should look like this:
{% if pageType == 'home' %}
i have a django 'templating' question
if i have in views.py:
def cv(request):
if request.user.is_authenticated():
cv = OpenCv.objects.filter(created_by=request.user)
return render_to_response('cv/cv.html', {
'object_list': cv,
},
context_instance=RequestContext(request))
and in cv.html something like:
{% for object in object_list %}
<li>
First Name {{ object.first_name }} <br />
Last Name {{ object.last_name }} <br />
Url {{object.url}} <br />
Picture {{object.picture}} <br />
Bio {{object.bio}} <br />
Date of birth {{object.date_birth}} <br />
{% endfor %}
but i want this content to appear on the profile.html page too, how can i do it?
a smple {% include "cv/cv.html" %} in the profile.html doesn't work.
Also, is there another way to 'parse the object list' than explicitly write all the objects, like above?
thanks in advance!
You need to pass object_list to profile.html as well.
It's not very clear what you're asking, but I suspect a custom template tag might be what you're after.