Enhanced Version of HAML (Less typing, php-like variable syntax)
Requires PHP >= 8.1
composer require cseufert/hamle dev-master
This haml port uses slightly different syntax to the haml standard format. The main idea here is to reduce the number of symbols that are typed. The second issue this port is attempting to address, is to remove native code from the template. The idea here being that your template could be served as html from the server, however where supported, it could be rendered by javascript, with a small json request to retreive the data to fill the template with. The main focus for hamle is not on markup, but on page/document structure, so inline tags are not a consideration at this stage. The focus is on clean, readable markup
.myclass
, P.quote
,A#home
, #content.cols.two
)
.one Text
becomes <div class="one">Text</div>
span.two Foo
becomes <span class="two">Foo</span>
#my.mine.ours
becomes <div id="my" class="mine ours"></div>
html
body
div#content
a[href=/] Home
becomes
<html>
<body>
<div id="content">
<a href="/">Home</a>
</div>
<body>
</html>
DIV
, P
, A
, etc)
a[href=/] Home
outputs <a href="/">Home</a>
.button[data-size=$id&class=$tags]
if $id is 10 and tags = 'Ten Submit' output would be <button data-size="10" class="Ten Submit" />
a[data-alt=Button \[1\&2\]] Hi
becomes <a data-alt="Button [1&2]">Hi</a>
$title
, $text
, {$[-1]->title}
, etc)
{$[1]-!title}
or {$(page.home)-!title}
{$...}
over $...
are required when inside a filter block, or accessing a property.$[0]
= current model/controller that is in scope
|with $[-1]
- Switch back to last scope{$[1]->title}
- read value $title
from initial scope$[-1]
= Last Scope ; Array array of scopes $[1]
first scope, $[-2]
second last scope|with $(page.footer) as footer
$[footer]
|each $[footer]
{$[footer]->html}
$
function
$({<type>}{@<group>}{#<id>}{.<tags>}{^<sort>}{:{<offset>-}<limit>})
<type>
is a type that hamleSetup->modelType($type) can find<group>
is an arbitary id that determines a group type, eg. for differentiating gallery image from header image<id>
is a unique id, either combined with a type, or globally unique<tags>
are user defined tags that can be used to help find find data<sort>
field to sort on, by default ascending, prefix with - for^-sort^title
.<limit>
Limit results to n<offset>
Number of results to skip before return<type>
or <id>
, depending on implementation of modelFind$(#1024)
opens id = 1024$(#mine)
opens object with id/alias = mine$(page#3)
opens page type with id = 3$(cat)
opens a list of all category objects$(product.onsale)
opens a list of all products with onsale tag$(cart#summary)
open summary item from cart$(#mainmenu > page,cat)
returns list of all children of id = mainmenu that are type pages, and cats$( > photo, image)
return list of all photos and images who are children of current scope$( < cat)
returns all parents of type category within the current scope$(page:1)
return first 1 page from pages$( > page,cat:3)
return 3 pages or cats (in total) that are children of whats in scope$(news^postdate)
returns all news posts sorted ascending by postdate$(news^-postdate)
returns all news posts sorted descending by postdate$(news^)
returns all news posts sorted in random order$(news^-postdate^title)
returns all news posts sorted by date (DESC), then title(ASC)$(link:5-10)
returns links starting at #5 through 10$(news:4)
return 4 news posts$(product.featured:4^)
Return 4 randomly selected products with featured tag$(post:4^postdate)
return first 4 blog posts ordered by postdate$(post^-postdate:1)
return most recent blog post|with $(#mainmenu > page)
- changes M/C scope to pages under mainmenu, if no results skips section|each
- iterates through each object in the current scope (set by |with)|each $(#social > icons)
- iterate through children icons from id = social|include "block/$type/list.hamle"
- bring another hamle file into the doc, with the current M/C scope
|if $id equals $(view)->id
- include section if this id is the view id
if $title equals a
if $id notequal 54
if $tags has sale
- has sale in arrayif $title starts Hi
if $title ends s
if $title contains Hi
- Contains the string Hiif $price greater 10
if $price less 10
|else
- else for |with
, and |if
|page <key>,<size> <modelIterator>
- eg |page results,16 $(#gallery > photo)
a!firstpage
; a!prevpage
; a!nextpage
; a!lastpage
;div!thispage
of div!pagecount
; div!pagelinks
;|recurse $( > menu,page) #3
Recurse up to 3 levels deep using expression providedif $price less 10 OR $price greater 20
|unless $title
- if not shortcut, show block if there is no title|switch $type
- switch based on $type
|case page
- include section if case matches|default
- include section if none of the cases matches|iterateend
- iterate until end of list
|iterate 3
- iterate 3 times / Print a table with 3 column of all the products in the current scope (eg category)
|with $( > products)
table
|iterateend
tr
|iterate 3
td
a[href=$url] $title
|with $( > photo)
img[src=$url]
|else
td
:filtername
- Use filter named filtername to process section):javascript alert('Hi {$(user)->name}');
:css a {color:{$(site)->linkcolor}}
{$
sequence in css/javascript/etc, you just escape the dollar eg. {$
// Comment
- not included in output/ Comment
- included as HTML comment_ This is just plain text
_
is only required when text is the first thing on a new line__ <!doctype html>
to print html5 doctype___
when you want unescape html (include variables) This allows rendering variables that contain html to display as html{$title|strtoupper}
runs strtoupper on models title value{$amount|format_currency("$", $(#prefs)->places)}
Pass arguments to functionshtml
body
.head
h1 This is my website
img[src=/img/$imagename.png]
.content
.h2 $title
|with $(#1012 > text)
ul#mainmenu
|each
li.menuitem
|if {$[1]->id} = $id
a.highlight[href=$url] $title
|else
a[href=$url] $title
|with $( > text)
ul.submenu
|each
li.menuitem
|if {$[1]->id} = $id
a.highlight[href=$url] $title
|else
a[href=$url] $title
.body
|if $text
$text
|else
p Nothing to see here. Page ID=$id is emtpy
.foot
ul.socialicons
|each $(#socialmedia > link)
li.icon
a[href=$url&class=$code]
img[alt=$title] $->child(“img”)->url
|include "footer_$type.hamle"
|if $alias != “home”
a[href=/] Home
.powered
$(site)->poweredby