GithubHelp home page GithubHelp logo

think-template's Introduction

ThinkTemplate

基于XML和标签库的编译型模板引擎

主要特性

  • 支持XML标签库和普通标签的混合定义;
  • 支持直接使用PHP代码书写;
  • 支持文件包含;
  • 支持多级标签嵌套;
  • 支持布局模板功能;
  • 一次编译多次运行,编译和运行效率非常高;
  • 模板文件和布局模板更新,自动更新模板缓存;
  • 系统变量无需赋值直接输出;
  • 支持多维数组的快速输出;
  • 支持模板变量的默认值;
  • 支持页面代码去除Html空白;
  • 支持变量组合调节器和格式化功能;
  • 允许定义模板禁用函数和禁用PHP语法;
  • 通过标签库方式扩展;

安装

composer require topthink/think-template

用法示例

<?php
namespace think;

require __DIR__.'/vendor/autoload.php';

// 设置模板引擎参数
$config = [
	'view_path'	=>	'./template/',
	'cache_path'	=>	'./runtime/',
	'view_suffix'   =>	'html',
];

$template = new Template($config);
// 模板变量赋值
$template->assign(['name' => 'think']);
// 读取模板文件渲染输出
$template->fetch('index');
// 完整模板文件渲染
$template->fetch('./template/test.php');
// 渲染内容输出
$template->display($content);

支持静态调用

use think\facade\Template;

Template::config([
	'view_path'	=>	'./template/',
	'cache_path'	=>	'./runtime/',
	'view_suffix'   =>	'html',
]);
Template::assign(['name' => 'think']);
Template::fetch('index',['name' => 'think']);
Template::display($content,['name' => 'think']);

详细用法参考开发手册

think-template's People

Contributors

9007967 avatar big-dream avatar liu21st avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

think-template's Issues

建议类的成员/方法可见性改为protected

能否把模板中的private方法改成protected
我项目中用到啦这个模板引擎但是我的模板路径是一个数组,我想到啦继承方法parseTemplateFile 修改查找模板的规则,但这个方法是private的
我看到源码要好多方法都是private的,能否把方法改成protected这样可以继承实现自己的逻辑,省得自己复制一份改源码
我是从thinkphp3.1就用框架啦,模板的使用方法已经很熟悉啦,很喜欢这个模板引擎

还是关于跨模块调用的问题

关于这个问题已经成为think-template的热搜了,希望流大大能支持下模板中的调用方法“模块@控制器/操作”的写法,保持和5.1一样够在当前应用下调用其他应用的模板。
以前我5.1的在模板中都这样写的 {extend name='admin@test'} {include file="admin@header" /} 这样写都可以,但6.0就不行了。没了@这个操作就需要写很多个 “../../../../../”这个的相对目录,太麻烦了。最后实在忍不住把Template类的parseTemplateFile改了成了下面这样:

    /**
     * 解析模板文件名
     * @access private
     * @param  string $template 文件名
     * @return string
     */
    private function parseTemplateFile(string $template): string
    {
        if ('' == pathinfo($template, PATHINFO_EXTENSION)) {

            if (0 !== strpos($template, '/')) {
                $template = str_replace(['/', ':'], $this->config['view_depr'], $template);
            } else {
                $template = str_replace(['/', ':'], $this->config['view_depr'], substr($template, 1));
            }
            // 获取视图根目录
            if (strpos($template, '@')) {
                // 跨模块调用
                list($app, $template) = explode('@', $template);
                $template = base_path($app) . basename($this->config['view_path']) . DIRECTORY_SEPARATOR .$template .  '.' . ltrim($this->config['view_suffix'], '.');
            }else {
                $template = $this->config['view_path'] . $template . '.' . ltrim($this->config['view_suffix'], '.');
            }
        }

        if (is_file($template)) {
            // 记录模板文件的更新时间
            $this->includeFile[$template] = filemtime($template);

            return $template;
        }

        throw new Exception('template not exists:' . $template);
    }

关于这个问题看云手册、官网帖子都发了很多,希望流大大能够稍微关注下。

模板中url标签的错误

return '<?php echo url("' . $url . '","' . $vars . '",' . $suffix . ',' . $domain . ');?>';

会导致错误:
#0 [0]TypeError in helper.php line 501
url(): Argument #2 ($vars) must be of type array, string given, called in

应该改为:
return '<?php echo url("' . $url . '",' . $vars . ',' . $suffix . ',' . $domain . ');?>';

包含文件的用法,在使用内置服务器时有问题

{include file="../application/view/default/public/header.html" /}

如果是正常的部署并且设置目录到public,那么这个包含文件是正常的,
此时是基于public目录开始的.
但如果是使用:

think php run

就会出现问题,就不是基于项目public目录而是项目根目录了.

包含文件传参数不支持对象

php变量也不行,必须在控制器定义变量view::assign('$title','标题');才能替换

{include file="Public/header" title="$info->title" /}
标题测试:[title]

结果
标题测试:

是否去除模板文件里面的html空格与换行 BUG修正

$replace = ['><', '>'];

问题描述:
在前面布局的时候,比如只需要一个空格的间距,通常不会用CSS样式去设置间距。例如两个A标签之间我只需要一个空格的间距,现在的情况是空格被全部替换了,导致两个A标签看起来像是一个A标签,因为A标签的默认样式被我用CSS初始化了。
修复前:
$replace = ['><', '>'];
修复后
$replace = ['> <', '>'];

临时关闭layout的预期有问题

         View::fetch('login/ext/ulthon_login');
         View::layout(false)->fetch('login/ext/ulthon_login');

如上面两个代码,第一行,预期会渲染应用下login控制器的ext/ulthon_login文件。

第二行应该也会渲染这个文件,只是第二行渲染出来的内容不包括layout才对。
但实际上,第二行会导致到不到文件,两行代码生成的template文件如下:

"/www/wwwroot/admin.demo.ulthon.com/app/admin/view/login/index.html"
"login/ext/ulthon_login"

并不能正确匹配到控制器下的文件,好像是因为layout方法返回$this,实际上是templat类,但实际上,应该返回think-view类。

可否设置模板中变量不存在的时候使用默认值?

请问如何设置当模板文件中使用的变量未定义时使用默认值而不是报错 undefined ?尝试使用 setting="setting|default=[]" 无效

另外,如何获取当前模板文件传入的变量列表?方便判断那些变量可用

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.