Skip to content

Instantly share code, notes, and snippets.

@NdYAG
Last active December 27, 2023 06:24
Show Gist options
  • Save NdYAG/8553425 to your computer and use it in GitHub Desktop.
Save NdYAG/8553425 to your computer and use it in GitHub Desktop.
Mako Template Sample

Mako Template Syntax Sample

variable, expression

${x}

${len(list_name)}

${pow(x,2) + pow(y,2)}

control flow

if else

% if x == 1:
<div class="">It's One</div>
% elif x == 2:
<div class="">It's Two</div>
% else:
<div class="">Others</div>
% endif

the whitespace between % and 'if else endif' etc., can be omitted.

so %if, %else, %elif, %endif is ok.

for

%for a in [1, 2, 3]:
  <li>${a}</li>
%endfor

Comments

Using ## to comment a line

## <div class="foo"></div>

Inline python codes

There are two kinds:

<%
## python code here
name = 'web-mode'
%>
<%!
  from datetime import datetime
%>

Other tags

self enclose

<%page args="" />
<%include file="" />
<%namespace file="" />
<%inhreit file="" />
<%call expr="" />

explicit closing tag

<%def name="alert()">
## code here
</%def>

<%block name="">
## code here
</%block>
@Eshan-Agarwal
Copy link

@NdYAG maybe this is not right place to ask but I can't find any solution so asking here , I am writing .mako.md file, can you please help me, here is part of my code :

<%!
  import requests
  def dataset_examples_paths(ds_name):
    github_path = "https://github.com/Eshan-Agarwal/" + ds_name + ".jpg"
    return github_path
  def example_exists(path):
    r = requests.head(path)
    return r.is_redirect
%>

problem is with github_path it takes url "https://github.com/Eshan-Agarwal/" with last " due to which it return 404 error as url displayed is this in web browser https://github.com/Eshan-Agarwal/" How can I get off this "
Thanks

@oronce
Copy link

oronce commented Nov 25, 2021

what kind of value ds_name contain ??
otherwise, my solution would be to store the github_path variable in a python file instead of the template and passed when rendering

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment