markdown EE与工艺的模板化

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown EE与工艺的模板化相关的知识,希望对你有一定的参考价值。

# Templating in EE vs. Craft CMS.

Here are a few common tasks you might do in your templates, as they would be written in ExpressionEngine vs. Craft CMS.

### Table of Contents

1. [Comments](#1-comments)
2. [Conditionals](#2-conditionals)
3. [Loops](#3-loops)
4. [Looping Through Entries](#4-looping-through-entries)
5. [Single-Entry Pages](#5-single-entry-pages)
6. [Looping Through Matrix Rows](#6-looping-through-matrix-rows)
7. [Looping Through Assets](#7-looping-through-assets)
8. [Template Inheritance](#8-template-inheritance)
9. [Template Includes](#9-template-includes)
10. [Stuff You Can Only Do in Craft](#10-stuff-you-can-only-do-in-craft)
11. [Other Resources](#11-other-resources)

----------------------------------------------------------------------------------------------------

## 1. Comments

Let’s start simple! Both CMSes support comment tags, enabling you to make notes in the template code
that never make it to the browser.

#### ExpressionEngine

```
{!-- You can’t see me! --}
```

#### Craft

```jinja
{# You can’t see me! #}
```

Resources:

- [Twig’s comment tag docs](http://twig.sensiolabs.org/doc/templates.html#comments)

----------------------------------------------------------------------------------------------------

## 2. Conditionals

#### ExpressionEngine

```
{if something == 'value'}
    ...
{if:elseif something_else == 'other_value'}
    ...
{if:else}
    ...
{/if}
```

#### Craft

```jinja
{% if something == 'value' %}
    ...
{% elseif somethingElse == 'otherValue' %}
    ...
{% else %}
    ...
{% endif %}
```

Resources:

- [Twig’s {% if %} tag docs](http://twig.sensiolabs.org/doc/tags/if.html)
- [Twig’s logical operator docs](http://twig.sensiolabs.org/doc/templates.html#logic)
- [Twig’s comparrisson operator docs](http://twig.sensiolabs.org/doc/templates.html#comparisons)

----------------------------------------------------------------------------------------------------

## 3. Loops

#### ExpressionEngine

EE loops are always done with tag pairs, named after what we’re looping through.

```
{exp:channel:entries channel="news"}
    {matrix_field}
        ...
    {/matrix_field}
{/exp:channel:entries}
```

#### Craft

Looping through things in Craft is always done with a
[for-loop](http://twig.sensiolabs.org/doc/tags/for.html), where you pass in what you want to loop
through, as well as what you want to call each item within the loop.

```jinja
{% for entry in craft.entries.section('news') %}
    {% for block in entry.matrixField %}
        ...
    {% endfor %}
{% endfor %}
```

There are pros and cons to both ways: EE’s is arguably more elegant for simple things, but you can quickly run into tag name conflicts and other gotchas when dealing with more complex templates with nested loops. That’s never an issue with Craft.

Both CMSes have various tags that are available within the loops, as well:

<table>
    <thead>
        <tr>
            <th>Thing</th>
            <th>ExpressionEngine</th>
            <th>Craft</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>How many items?</td>
            <td>{total_results}</td>
            <td>{{ loop.length }}</td>
        </tr>
        <tr>
            <td>1-based index</td>
            <td>{count}</td>
            <td>{{ loop.index }}</td>
        </tr>
        <tr>
            <td>0-based index</td>
            <td>-</td>
            <td>{{ loop.index0 }}</td>
        </tr>
        <tr>
            <td>Number of items left</td>
            <td>-</td>
            <td>{{ loop.revindex0 }}</td>
        </tr>
        <tr>
            <td>Number of ites left, inc. this one</td>
            <td>-</td>
            <td>{{ loop.revindex }}</td>
        </tr>
        <tr>
            <td>First item?</td>
            <td>{if count == 1}</td>
            <td>{% if loop.first %}</td>
        </tr>
        <tr>
            <td>Last item?</td>
            <td>{if count == total_results}</td>
            <td>{% if loop.last %}</td>
        </tr>
        <tr>
            <td>Odd?</td>
            <td>{if count % 2 == 1}</td>
            <td>{% if loop.index is odd %}</td>
        </tr>
        <tr>
            <td>Even?</td>
            <td>{if count % 2 == 0}</td>
            <td>{% if loop.index is even %}</td>
        </tr>
        <tr>
            <td>Cycle through values for each item</td>
            <td>{switch='odd|even'}</td>
            <td>{{ cycle(['odd','even'], loop.index0) }}</td>
        </tr>
        <tr>
            <td>The parent loop’s index</td>
            <td>-</td>
            <td>{{ loop.parent.loop.index }}</td>
        </tr>
    </tbody>
</table>

Resources:

- [Twig’s {% for %} tag docs](http://twig.sensiolabs.org/doc/tags/for.html)

----------------------------------------------------------------------------------------------------

## 4. Looping Through Entries

#### ExpressionEngine

ExpressionEngine’s `{exp:channel:entries}` tag is optimized for this task. Just tell it which channel, how many, and so on. (And don’t forget `dynamic="no"` on single-entry pages!)

```
{exp:channel:entries channel="news" limit="10"}
    <h2><a href="{path='news/{url_title}'}">{title}</a></h2>
    <p>{summary}</p>
{/exp:channel:entries}
```

#### Craft

In Craft you grab the entries using
[craft.entries](https://craftcms.com/docs/templating/craft.entries) and loop through them with
a [for-loop](http://twig.sensiolabs.org/doc/tags/for.html). 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..

```jinja
{% for newsEntry in craft.entries.section('news').limit(10) %}
    <h2><a href="{{ newsEntry.url }}">{{ newsEntry.title }}</a></h2>
    <p>{{ newsEntry.summary }}</p>
{% endfor %}
```

Resources:

- [craft.entries docs](https://craftcms.com/docs/templating/craft.entries)
- [EntryModel docs](https://craftcms.com/docs/templating/entrymodel)

----------------------------------------------------------------------------------------------------

## 5. Single-Entry Pages

#### ExpressionEngine

On templates EE thinks are for displaying a single entry, `{exp:channel:entries}` will only return that one entry by default – based on a URL Title match with the second URI segment.

```
{exp:channel:entries channel="news"}
    <h1>{title}</h1>
    {body}
{/exp:channel:entries}
```

#### Craft

Entries in Craft can have explicit URLs, so when one of those URLs is requested, Craft can confidently know 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. (Note that in this case the `entry` variable name is not customizable.)

```jinja
<h1>{{ entry.title }}</h1>
{{ entry.body }}
```

Resources:

- [Craft Routing docs](https://craftcms.com/docs/routing)

----------------------------------------------------------------------------------------------------

## 6. Looping Through Matrix Rows

#### ExpressionEngine

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}
```

#### Craft

As with looping through entries, we loop through Matrix blocks using a
[for-loop](http://twig.sensiolabs.org/doc/tags/for.html).

```jinja
{% for block in entry.matrixField %}
    {{ block.fieldOne }}
    {{ block.fieldTwo }}
{% endfor %}
```

If you have multiple block types, you can add conditionals for them:

```jinja
{% 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 %}
```

Resources:

- [MatrixBlockModel docs](https://craftcms.com/docs/templating/matrixblockmodel)

----------------------------------------------------------------------------------------------------

## 7. Looping Through Assets

#### ExpressionEngine

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}
```

#### Craft

Like entries and Matrix fields in Craft, we once again use the
[for-loop](http://twig.sensiolabs.org/doc/tags/for.html) to loop through assets:

```jinja
{% for asset in entry.assetsField %}
    <img src="{{ asset.url('transformHandle') }}" alt="{{ asset.title }}"> {{ asset.filename }}
{% endfor %}
```

Resources:

- [AssetFileModel docs](https://craftcms.com/docs/templating/assetfilemodel)

----------------------------------------------------------------------------------------------------

## 8. Template Inheritance

#### ExpressionEngine

EE 2.8 introduced [Template Layouts](https://docs.expressionengine.com/latest/templates/layouts.html), which makes it possible for templates to extend other templates, via the `{layout=}` tag:

###### site/.site-layout

```
<html>
<head>
    <title>{layout:title}</title>
</head>
<body>
    {layout:contents}

    <p class="copyright">
        &copy; {current_time format='%Y'} {site_name}
    </p>
</body>
</html>
```

###### news/index

```
    {layout="site/.site-layout"}
    {layout:set name="title" value="News"}

    {exp:channel:entries channel="news"}
        <h2>{title}</h2>
        {summary}
    {/exp:channel:entries}
```

#### Craft

The same is also possible in Twig:

###### \_site_layout.html

```jinja
<html>
<head>
    <title>{{ title }}</title>
</head>
<body>

    {% block body %}
        Default content
    {% endblock %}
    
    <p class="copyright">
        &copy; {{ now | date('Y') }} {{ siteName }}
    </p>
</body>
</html>
```

###### news/index.html

```jinja
{% extends "_site_layout" %}
{% set title = "News" %}

{% block body %}
    <h1>News</h1>
    ...
{% endblock %}
```

There is one key difference between EE and Twig here: In Twig, if a template extends another, all of its HTML output must be placed within `{% block %}...{% endblock %}` tags. If the template attepmts to output anything outside of block tags, Twig will complain about it. Whereas in EE, all of the HTML that is output gets sucked into a single “block” called `contents`, which is output in the layout template with `{layout:contents}`.

Resources:

- [Twig’s Template Inheritance docs](http://twig.sensiolabs.org/doc/templates.html#template-inheritance)

----------------------------------------------------------------------------------------------------

## 9. Template Includes

#### ExpressionEngine

You can include other templates in EE using the `{embed}` tag, passing any variables you want available to the embedded template as parameters:

###### Parent Template:

```
{embed="includes/_photo_gallery" entry_id="{entry_id}"}
```

###### includes/\_photo_gallery:

```
{exp:channel:entries entry_id="{embed:entry_id}" dynamic="no"}
    <div class="gallery">
        {assets_field}
            <img src="{url:manipulation_name}">
        {/assets_field}
    </div>
{/exp:channel:entries}
```

#### Craft

Twig has a similar tag, called [`{% include %}`](http://twig.sensiolabs.org/doc/tags/include.html).
Unlike `{embed}`, all variables that are already defined in the template leading up to the
`{% include %}` tag will automatically be available to the included template.

###### Parent Template:

```jinja
{% include "_includes/photo_gallery" %}
```

###### \_includes/photo_gallery:

```jinja
<div class="gallery">
    {% for photo in entry.assetsField %}
        <img src="{{ photo.url('transformHandle') }}">
    {% endfor %}
</div>
```

Note how `entry` was already available to \_includes/photo_gallery, since it was presumably already
defined by the parent template.

You also have the option to pass additional variables to the included template that weren’t already
defined in the parent. In the above example, we might not want the include template to worry about
the entry variable’s name, and just be passed the array of images directly. We could do do that with
this syntax:

###### Parent Template:

```jinja
{% include "_includes/photo_gallery" with {
    photos: entry.assetsField
} %}
```

###### \_includes/photo_gallery:

```jinja
<div class="gallery">
    {% for photo in photos %}
        <img src="{{ photo.url('transformHandle') }}">
    {% endfor %}
</div>
```

Now the include template is expecting to be passed a `photos` variable, and it’s up to whoever’s
including it to ensure that `photos` is set properly. Thanks to that `with` parameter, we can do
that without having to make a `photos` variable available to the parent template.

### The {% embed %} Tag

Twig also has an [`{% embed %}`](http://twig.sensiolabs.org/doc/tags/embed.html) tag, which you can think of as a more dynamic version of `{% include %}`. In addition to including a template into the parent template, it also allows the parent template to override aspects of the included template, using `{% block %}` tags.

Here’s a simple example, where a photo gallery include template allows its parent templates to override a header area:

###### Parent Template:

```jinja
{% embed "_includes/photo_gallery" %}
    {% block header %}
        <h3>The Best Photo Gallery</h3>
    {% endblock %}
{% endembed %}
```

###### \_includes/photo_gallery:

```jinja
{% block header %}
    <h3>Photo Gallery</h3>
{% endblock %}

<div class="gallery">
    {% for photo in entry.assetsField %}
        <img src="{{ photo.url('transformHandle') }}">
    {% endfor %}
</div>
```


Resources:

- [Twig’s {% include %} docs](http://twig.sensiolabs.org/doc/tags/include.html)
- [Twig’s {% embed %} docs](http://twig.sensiolabs.org/doc/tags/embed.html)

----------------------------------------------------------------------------------------------------

## 10. Stuff You Can Only Do in Craft

All of the above examples have been focused on things that are relatively easy to do in both EE and
Craft. But thanks to Twig, there are a ton of things you can easily do in Craft that aren’t even
possible in EE out of the box.

#### Set Custom Variables

You’re not stuck with a limited set of variable tags in Craft. Thanks to Twig, you can define new
variables right in your templates, manipulate them, and output them as you see fit.

```jinja
{% set title = "About Us" %}

<html>
<head>
    <title>{{ title }} - {{ siteName }}</title>
</head>
<body>
    <h1>{{ title | upper }}</h1>
</body>
</html>
```

Resources:

- [Twig’s {% set %} tag docs](http://twig.sensiolabs.org/doc/tags/set.html)
- [Twig’s filter docs ](http://twig.sensiolabs.org/doc/templates.html#filters)

#### Advanced Math

All sorts of advanced math operations are possible in Twig thanks to its rich support of [mathematical operators](http://twig.sensiolabs.org/doc/templates.html#math)

#### String Manipulations

You can concatenate strings, modify them, split them into arrays, or pretty much anything else you
can think of.

```jinja
{% if currentUser %}
    {% set greeting = "Hello, " ~ currentUser.name %}
{% else %}
    {% set greeting = "Nice to meet you." %}
{% endif %}

{% set totalWords = greeting | split(' ') | length %}
{% set greeting = greeting ~ ' (That was '~totalWords~' words!)' %}

{# Output the greeting in all caps #}
{{ greeting | upper }}
```

Resources:

- [Twig’s {% set %} tag docs](http://twig.sensiolabs.org/doc/tags/set.html)
- [Twig’s filter docs ](http://twig.sensiolabs.org/doc/templates.html#filters)

#### Create Arrays

You aren’t limited to creating simple numbers and strings. You can even create full-blown arrays:

```jinja
{% set nav = [
    { uri: '', title: 'Home' },
    { uri: 'about', title: 'About' },
    { uri: 'products', title: 'Products' },
    { uri: 'blog', title: 'Blog' }
] %}

<nav>
    <ul>
        {% for item in nav %}
            {% set sel = (craft.request.path == item.uri) %}
            <li><a {{ sel ? 'class="sel"' }} href="{{ url(item.uri) }}">{{ item.title }}</a></li>
        {% endfor %}
    </ul>
</nav>
```

There’s also a shorthand syntax for creating arrays of consecutive numbers:

```jinja
<select name="cc_exp_year">
    {% for year in now.year .. now.year+10 %}
        <option>{{ year }}</option>
    {% endfor %}
</select>
```

----------------------------------------------------------------------------------------------------

## 11. Other Resources

If you want to learn more, here are a few helpful templating resources:

- [Twig for Template Designers](http://twig.sensiolabs.org/doc/templates.html) – the official Twig documentation
- [Twig Templates in Craft](https://mijingo.com/products/screencasts/twig-templates-in-craft/) – Video course by Mijingo
- [Control Flow in Twig](https://mijingo.com/products/screencasts/control-flow-in-twig/) – Video course by Mijingo
- [Twig’s core tags](http://twig.sensiolabs.org/doc/tags/index.html)
- [Twig’s core filters](http://twig.sensiolabs.org/doc/filters/index.html)
- [Twig’s core functions](http://twig.sensiolabs.org/doc/functions/index.html)
- [Craft-added tags](https://craftcms.com/docs/templating/tags)
- [Craft-added filters](https://craftcms.com/docs/templating/filters)
- [Craft-added functions](https://craftcms.com/docs/templating/functions)
- [Global template variables in Craft](https://craftcms.com/docs/templating/global-variables)

以上是关于markdown EE与工艺的模板化的主要内容,如果未能解决你的问题,请参考以下文章

告诉与HDI相关的知识与工艺

铸件成型工艺设计

C#怎么实例化对象?具体是实例化啥?

markdown 文本 模板

Markdown初始模板

markdown 文本 模板