Hunt Framework D语言hunt框架html常用模板语法

· Created · Last modified by Brian replied at · 239 times read

1.模板语法for循环

控制器中分配:
view.assign("channelAppidLength", channelAppidLength);
view.assign("channelAppids", channelAppidData);
view.assign("pid", pid);


html中渲染:

<select name="parent_id" class="form-control">
    <option value="0">无</option>
    {% if channelAppidLength == 1 %}
        {% for channelAppid in channelAppids %}
            <option value="{{channelAppid.id}}" {% if channelAppid.id == pid %} selected {% endif %} >{{channelAppid.name}}</option>
        {% endfor %}
    {% endif %}
</select>

2. 在html中显示一个变量,字符串变量

控制器中分配:
view.assign("name", "张三");


html中渲染:
<span>{{name}}</span>

3. 在html中解析对象变量

model文件定义个数据结构

class WechatMaterial
{
    int id;

    string appid;

    string media_id;

    string type;
}

控制器中实例化并赋值,分配:

wechatMaterial = new WechatMaterial();
wechatMaterial.id = 1;
wechatMaterial.appid = "xxxxxx";
wechatMaterial.media_id = "xccccc";
wechatMaterial.type = "a";
view.assign("material", wechatMaterial);


html中渲染:
<span>{{material.id}}</span>
<span>{{material.appid}}</span>
<span>{{material.media_id}}</span>
<span>{{material.type}}</span>

4. 在html使用if

if语句使用

控制器数据参考第三条分配;


html中渲染:
<option value="text" {% if material.type == "a" %} selected {% endif %}  >


if else 使用

注意:article 是控制器分配的对象,参考第三条使用

{% if article.status == 1 %}
    <input type="radio" id="custom-radio1" name="customradio" value="1" checked class="custom-control-input">
{% else %}
    <input type="radio" id="custom-radio1" name="customradio" value="1" class="custom-control-input">
{% endif %}

5.引入其他html文件


{% include "../../header.html" %}
    <div class="wrapper">
        ...
        ...
    </div>
{% include "../../footer.html" %}

6.html中生成一个url路径


配置文件中:
...
hunt.route.groups = admin:directory:admincp
...

路由文件定义:

...
GET  /wechat/channel/add     wechat.channel.add
...

控制器分配:
view.assign("pid", 100);

html中使用 url方法:
<a href="{{ url("wechat.channel.add", "")}}?pid={{pid}}" class="btn btn-success waves-effect waves-light">Add
    <i class="mdi mdi-plus-circle-outline"></i>
</a>

解析后
<a href="/admincp/wechat/channel/add/?pid=13469" class="btn btn-success waves-effect waves-light">Add
    <i class="mdi mdi-plus-circle-outline"></i>
</a>

的确很像 twig 和 jinja2 的语法吧,不错。

Login to reply