# set with 语句

###set语句:
在模板中, 可以使用 ‘set’语句来定义变量, 实例如下:
1 <body>
2     {% set username='zhiliaoketang' %}
3     <p>用户名: {{ username }}</p>
4 </body>

 

### `with`语句:
`with`语句定义的变量,只能在`with`语句块中使用,超过了这个代码块,就不能再使用了。示例代码如下:

1 {% with classroom='yiban' %}
2     <p>班级: {{ classroom }}</p>
3 {% endwith %}

`with`语句也不一定要跟一个变量,可以定义一个空的`with`语句,以后在`with`块中通过`set`定义的变量,就只能在这个`with`块中使用了:

1 ```html
2 {% with %}
3 {% set classroom = 'yiban' %}
4 <p>班级:{{ classroom }}</p>
5 {% endwith %}
6 ```

 

相关文章:

  • 2021-08-31
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2018-05-14
  • 2021-11-01
猜你喜欢
  • 2021-09-14
  • 2022-12-23
  • 2021-09-21
  • 2022-12-23
  • 2021-07-10
相关资源
相似解决方案