Lots of people have asked, so here are a few common tasks you might do in your templates, as they would be written in ExpressionEngine vs. Craft.
ExpressionEngine’s {exp:channel:entries}
tag is optimized for this task (assuming you’re not on a single-entry page!). Just tell it which channel, how many, and so on.
{exp:channel:entries channel="news" limit="10"}
<h2><a href="{path='news/{url_title}'}">{title}</a></h2>
<p>{summary}</p>
{/exp:channel:entries}
In Craft you grab the entries using craft.entries and loop through them with a for-loop. You get to choose a variable name that each entry is going to be set to. In this case we’re going with newsEntry
so it’s clear which ‘title’ we’re outputting, etc..
{% for newsEntry in craft.entries.section('news').limit(10) %}
<h2><a href="{{ newsEntry.url }}">{{ newsEntry.title }}</a></h2>
<p>{{ newsEntry.summary }}</p>
{% endfor %}
See EntryModel to see what you can do with those entry variables.
Assuming everything’s in the right place, this is the one time the dynamic
parameter will actually help you out, saving you from having to type url_title="{segment_2}" limit="1"
(Whew!).
{exp:channel:entries channel="news"}
<h1>{title}</h1>
{body}
{/exp:channel:entries}
Entries in Craft have their own URLs, so Craft knows for a fact which entry you’re trying to access, and which template it should load. In the process it will pass an entry
variable, pre-set to the entry you’re accessing. (In this case you don’t get a choice on what the entry
variable name is going to be called.)
<h1>{{ entry.title }}</h1>
{{ entry.body }}
Matrix follows the standard EE fieldtype convention of parsing a tag pair based on the field’s short name:
{matrix_field}
{column_one}
{column_two}
{/matrix_field}
As with looping through entries, we loop through Matrix blocks using a for-loop.
{% for block in entry.matrixField %}
{{ block.fieldOne }}
{{ block.fieldTwo }}
{% endfor %}
If you have multiple block types, you can add conditionals for them:
{% for block in entry.matrixField %}
{% if block.type == "text" %}
{{ block.textField }}
{% elseif block.type == "quote" %}
<blockquote>{{ block.quoteField }}</blockquote>
<p>– {{ block.authorField }}</p>
{% endif %}
{% endfor %}
See MatrixBlockModel to see what you can do with those Matrix block variables.
Like Matrix/EE, Assets uses a tag pair based on the field’s short name:
{assets_field}
<img src="{url:image_manipulation_name}" alt="{title}"> {filename}
{/assets_field}
Like entries and Matrix fields in Craft, we once again use the for-loop to loop through assets:
{% for asset in entry.assetsField %}
<img src="{{ asset.url('transformHandle') }}" alt="{{ asset.title }}"> {{ asset.filename }}
{% endfor %}
See AssetFileModel to see what you can do with those asset variables.
In EE you would do this with two embedded templates, which you’d manually include in every normal template.
<html>
<head>
<title>{if embed:page_name}{embed:page_name} - {/if}{site_name}</title>
</head>
<body>
<p class="copyright">
© {current_time format='%Y'} {site_name}
</p>
</body>
</html>
{embed="includes/_header" page_name="News"}
<h1>News</h1>
...
{embed="includes/_footer"}
Craft has the concept of Template Inheritance, which lets you define all of the common site elements in a single template, and extend it with sub-templates where necessary.
<html>
<head>
<title>{% if pageName is defined %}{{ pageName }} - {% endif %}{{ siteName }}</title>
</head>
<body>
{% block body %}
Default content
{% endblock %}
<p class="copyright">
© {{ now | date('Y') }} {{ siteName }}
</p>
</body>
</html>
{% extends "_site_layout" %}
{% set pageName = "News" %}
{% block body %}
<h1>News</h1>
...
{% endblock %}
this should be on the craft docs yeah?