I need a nested if-statement inside a range in a golang "html/template".
Bookmarks.html
{{define "bookmarks"}}
<div class="bookmarks">
{{range .}}
<div class="bookmark">
<div class="image">
<a target="_blank" rel="noopener noreferrer" href="{{.Url}}">
{if .Img }}
<img src="/bookmark/get/image?name={{ .Img }}"/>
{{else}}
<img src="../static/images/placeholder.jpg"/>
{{end}}
</a>
</div>
{{end}}
</div>
{{end}}
go run ends up in:
panic: template: bookmarks.html:26: unexpected {{end}}
Related
I am working on a django product overview page.
I display categories with a listview. I use a Bootstrap grid with two columns to display the categories as follows:
picture | Info
I now ant every 2nd column to be mirrored so the end resukt will be like this:
Picture | Info
Info | Picture
Picture | Info
How do I run a loop to make this work? My code looks like this:
<div class='container'>
{% for category in categories %}
<!-- Check is subcategory exists. if not, filter on category. If it does exist filter on subcategory -->
{% if category.sub_category is Null %}
<a href="{% url 'academy:brandByCat_list' category.category_name %}">
<div class="row py-3 item-display">
<div class='col-md item-img'>
<img src= {{category.category_picture.url}} class="img-fluid category-picture">
</div>
<div class="col-md">
<h1 class='py-3'>{{category.category_name}}</h1>
<p>{{category.category_info}}
</div>
</div>
</a>
{% else %}
<a href="{% url 'academy:brandBySubCat_list' category.sub_category %}">
<div class="row py-3 item-display">
<div class='col-md item-img'>
<img src= {{category.category_picture.url}} class="img-fluid category-picture">
</div>
<div class="col-md">
<h1 class='py-3'>{{category.sub_category}}</h1>
<p>{{category.category_info}}</p>
</div>
</div>
</a>
{% endif %}
{% endfor %}
Thanks for the help!
I figured it out!
By using a nested forloop.counter|divisibleby:'2' i managed to make it work.
{% for category in categories %}
<!-- Check is subcategory exists. if not, filter on category. If it does exist filter on subcategory -->
{% if category.sub_category is Null %}
{% if forloop.counter|divisibleby:'2' %}
<a href="{% url 'academy:brandByCat_list' category.category_name %}">
<div class="row py-3 item-display">
<div class='col-md item-img'>
<img src= {{category.category_picture.url}} class="img-fluid category-picture">
</div>
<div class="col-md">
<h1 class='py-3'>{{category.category_name}}</h1>
<p>{{category.category_info | linebreaks}}
</div>
</div>
</a>
{% else %}
<a href="{% url 'academy:brandByCat_list' category.category_name %}">
<div class="row py-3 item-display">
<div class="col-md">
<h1 class='py-3'>{{category.category_name}}</h1>
<p>{{category.category_info | linebreaks}}
</div>
<div class='col-md item-img'>
<img src= {{category.category_picture.url}} class="img-fluid category-picture">
</div>
</div>
</a>
{% endif %}
{% else %}
{% if forloop.counter|divisibleby:'2' %}
<a href="{% url 'academy:brandBySubCat_list' category.sub_category %}">
<div class="row py-3 item-display">
<div class='col-md item-img'>
<img src= {{category.category_picture.url}} class="img-fluid category-picture">
</div>
<div class="col-md">
<h1 class='py-3'>{{category.sub_category}}</h1>
<p>{{category.category_info | linebreaks}}</p>
</div>
</div>
</a>
{% else %}
<a href="{% url 'academy:brandBySubCat_list' category.sub_category %}">
<div class="row py-3 item-display">
<div class="col-md">
<h1 class='py-3'>{{category.sub_category}}</h1>
<p>{{category.category_info | linebreaks}}</p>
</div>
<div class='col-md item-img'>
<img src= {{category.category_picture.url}} class="img-fluid category-picture">
</div>
</div>
</a>
{% endif %}
{% endif %}
{% endfor %}
This is the template I've written
{% extends 'base.html' %}
{% load static %}
{% block body %}
<div class="row hide-on-mobile" style="height: 100vh">
<div class="col-auto p-5">
<img src="{% static 'logo.svg' %}" class="mb-5 hide-on-mobile" style="height: 84px"/>
{% block form %} {% endblock %}
</div>
<div class="col bg-brown p-5">
<div class="d-flex flex-column justify-content-between" style="height: 100%">
<h1 class="text-blue mb-3">It's always Ups<br>and Downs</h1>
<img src="{% static 'investing_illustration.svg' %}" class="auth-illustration"/>
</div>
</div>
</div>
<div class="hide-on-desktop" style="height: 100vh; position: relative">
<div class="d-flex flex-column bg-brown h-100 p-3 align-items-start">
<img src="{% static 'logo.svg' %}" class="mb-3 hide-on-desktop" style="height: 56px"/>
<p class="text-blue mb-3">It's always Ups<br>and Downs</p>
<img src="{% static 'investing_illustration.svg' %}" class="auth-illustration"/>
</div>
<div style="position: absolute; bottom: 0; z-index: 99; background: white; width: 100%">
<div class="d-flex flex-column p-4">
<p class="mb-4">Login</p>
{% block form %} {% endblock %}
</div>
</div>
</div>
{% endblock %}
I inherit this template in a page as follows:
{% extends 'auth_base.html' %}
{% block form %}
<p class="mb-4">Login</p>
<form class="pb-5">
<input type="text" class="form-control" name="email" placeholder="Enter your email">
<input type="password" class="form-control" name="password" placeholder="Enter your password">
<a class="text-blue">Forgot Password?</a>
<button type="submit" class="mt-3 btn btn-primary full-width">Login</button>
</form>
<div class="text-center">
<span>New Here? <a class="text-blue">Create an account</a></span>
</div>
{% endblock %}
The form block is used two times..because when I inherit the template I want to write only once..but display it in two different places in the same page. In this case, I've written different layouts for mobile and desktop show the user actually sees only once.
However, this code doesn't work and gives the following error:
django.template.exceptions.TemplateSyntaxError: 'block' tag with name 'form' appears more than once
How can I accomplish this?
It is possible to include partials multiple times. Partials work differently then blocks - not via inheritence, so you would have to change your template code a bit.
You would need to create a partial template for your form which would expect a certain context (the form probably), and you would include that template snippet twice in your main template:
<div class="d-flex flex-column p-4">
<p class="mb-4">Login</p>
{% include "_partials/login_form.html" %}
</div>
_partials/login_form.html would basically contain what you had now inside your form block.
https://docs.djangoproject.com/en/3.1/ref/templates/builtins/#include
EDIT: as someone else pointed out - the question on multiple blocks with the same name has already been addressed before: https://stackoverflow.com/a/6427336/8401179
{%for x in dest%}
<div class="destination item">
<div class="destination_image">
<img src="{{baseurl}}/{{x.image}}" alt="">
{% if x.price > 4000%}
<div class="spec_offer text-center">Special Offer</div>
{%endif%}
</div>
<div class="destination_content">
<div class="destination_title">{{x.name}}</div>
<div class="destination_subtitle"><p>{{x.desc}}</p></div>
<div class="destination_price">{{x.price}}</div>
</div>
</div>
{% endfor %}
What is the problem at if statement above?? i tried to access the line special offer but error says couldnot parse the remainder.
I've this html page. I'm trying to extract the following information of this div:
<div class="clearfix">
<div class="container left">
<div class="logo">
<a href="/teams/belarus/fc-bate-borisov/200/">
<img src="http://cache.images.core.optasports.com/soccer/teams/150x150/200.png" alt="FC BATE Borisov" />
</a>
</div>
</div>
<div class="container middle">
<div class="details clearfix">
<dl>
<dt>Gara</dt>
<dd>Premier League</dd>
<dt>Data</dt>
<dd><span class='timestamp' data-value='1466877600' data-format='d mmmm yyyy'>25 giugno 2016</span></dd>
<dt>Game week</dt>
<dd>14</dd>
<dt>calcio di inizio</dt>
<dd>
<span class='timestamp' data-value='1466877600' data-format='HH:MM'>20:00</span>
(<span class="game-minute">FP'</span>)
</dd>
</dl>
</div>
<div class="details clearfix">
<dl>
<dt>Stadio</dt>
<dd>Borisov Arena (Barysaw (Borisov))</dd>
</dl>
</div>
</div>
<div class="container right">
<div class="logo">
<a href="/teams/belarus/fc-vitebsk/204/">
<img src="http://cache.images.core.optasports.com/soccer/teams/150x150/204.png" alt="FC Vitebsk" />
</a>
</div>
</div>
</div>
</div>
</div>
</div>
in particular the tab calcio di inizio - game week - stadio
Actually I've tried this regex: <div[^<>]*class="clearfix"[^<>]*>(?<content>.*?)
but when I test it on https://regex101.com/ I can't run the regex.
I think that the class of the div is associated on multiple divs, so this could be the problem.
And also the doesn't have any class for take it, any idea?
If you add an id to the div you want to get the contents of (for example "myDiv"), you could run the following javascript function to return it's HTML contents:
document.getElementById("myDiv").innerHTML
I am not exactly sure if this is what you want, since its not regex, but if so, I hope this helps!
Why I can't thing a view like part of a module?.
In .NET you have the view and the code behind. Sometimes we need to do somehing that match with this logic like grid with a widget inside each cell. Usually a widget have a little nice box with a title and the content, with a little logic, how I can include a partial like that to another view like that.
#extends('jarvis.admin._layouts.default')
#section('title')
Dashboard
#stop
#section('main')
<div class="row-fluid">
<div class="span4">
#yield('first')
<div class="jarviswidget" id="widget-id-00">
<header>
<h2>{{ $widget['title'] }}</h2>
</header>
<div>
<div class="jarviswidget-editbox">
<div>
<label>{{ ucfirst(Lang::get('strings.title')) }}:</label>
<input type="text">
</div>
<div>
<label>{{ ucfirst(Lang::get('strings.style')) }}</label>
<span data-widget-setstyle="red" class="red-btn"></span>
<span data-widget-setstyle="green" class="green-btn"></span>
<span data-widget-setstyle="purple" class="purple-btn"></span>
<span data-widget-setstyle="black" class="black-btn"></span>
<span data-widget-setstyle="darkgrey" class="darkgrey-btn"></span>
</div>
</div>
<div class="jarviswidget-timestamp"></div>
<div class="inner-spacer">
<!-- content goes here -->
#yield('wg_content')
Content
</div>
</div>
</div>
</div>
<div class="span4">
#yield('second')
<div class="jarviswidget" id="widget-id-00">
<header>
<h2>{{ $widget['title'] }}</h2>
</header>
<div>
<div class="jarviswidget-editbox">
<div>
<label>{{ ucfirst(Lang::get('strings.title')) }}:</label>
<input type="text">
</div>
<div>
<label>{{ ucfirst(Lang::get('strings.style')) }}</label>
<span data-widget-setstyle="red" class="red-btn"></span>
<span data-widget-setstyle="green" class="green-btn"></span>
<span data-widget-setstyle="purple" class="purple-btn"></span>
<span data-widget-setstyle="black" class="black-btn"></span>
<span data-widget-setstyle="darkgrey" class="darkgrey-btn"></span>
</div>
</div>
<div class="jarviswidget-timestamp"></div>
<div class="inner-spacer">
<!-- content goes here -->
#yield('wg_content')
Content
</div>
</div>
</div>
</div>
<div class="span4">
#yield('third')
<div class="jarviswidget" id="widget-id-00">
<header>
<h2>{{ $widget['title'] }}</h2>
</header>
<div>
<div class="jarviswidget-editbox">
<div>
<label>{{ ucfirst(Lang::get('strings.title')) }}:</label>
<input type="text">
</div>
<div>
<label>{{ ucfirst(Lang::get('strings.style')) }}</label>
<span data-widget-setstyle="red" class="red-btn"></span>
<span data-widget-setstyle="green" class="green-btn"></span>
<span data-widget-setstyle="purple" class="purple-btn"></span>
<span data-widget-setstyle="black" class="black-btn"></span>
<span data-widget-setstyle="darkgrey" class="darkgrey-btn"></span>
</div>
</div>
<div class="jarviswidget-timestamp"></div>
<div class="inner-spacer">
<!-- content goes here -->
#yield('wg_content')
Content
</div>
</div>
</div>
</div>
</div>
<div class="row-fluid">
<div class="span12">
</div>
</div>
#stop
Where each #yield will load a small widget processing the little logic or even another template with another nested views.
For example #yield('first') will load a box that is another template with a yield inside, and a variable as title. This nested yield will have another yield or the content... then the same box we use for the box will be used another time to render #yield('second') with another title and another content, that maybe use the same as the first yield.
I don't understand how to make this cascade with blade template system. Is there a way to do something similar?
I know is complicated but if you ever used .net, you could understand what I mean.
Thanks and sorry for my english.
Do that have similarities with the HMVC model, but is not the same.
I found the solution...
Is a little complicated to explain here but I'm going to do my best.
Try this:
default template (default.blade.php)
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<div class="fluid-container">
#yield('main')
</div>
</body>
</html>
default content template (mypage.blade.php)
#extends('_layouts.default')
#section('main')
<div id="widget-grid">
<div class="row-fluid">
<article class="span12">
#include('_modules.mymodule')
</article>
</div>
</div>
#stop
module template (mymodule.blade.php)
We send some variables to the shared template widget
#extends('admin._partials.widget', array('widget' => array('title' => 'title', 'id' => 'some_id', 'style' => 'your: style')))
#section('wg_content')
<!-- Your widget content goes here -->
#overwrite
shared template (widget.blade.php)
Your title $widget['title']
Your id $widget['id']
Your style $widget['style']
<!-- content -->
#yield('wg_content')
The views directory tree would be:
views
_layouts
default.blade.php
_partials
widget.blade.php
_modules
mymodule.blade.php
page
mypage.blade.php
Now when you make the view from the controller you should call mypage.
The magic directive is #overwrite in the bottom of module template.
Now when you call the module, the system get the widget template and wrap mymodule, then add this to the page and finally wrap everything within default which is the main template.
This help you to share a template with others. I know you have question, ASK! XD