doupijin0397 2011-02-04 22:11
浏览 62
已采纳

在SMARTY 3中使用包含模板的文件名默认值

Although I don't think the question was as good as it could be, let me try to explain better here.

I have a site using SMARTY 3 as the template system. I have a template structure similar to the below one:

/templates/place1/inner_a.tpl
/templates/place1/inner_b.tpl

/templates/place2/inner_b.tpl
/templates/place2/inner_c.tpl

/templates/default/inner_a.tpl
/templates/default/inner_b.tpl
/templates/default/inner_c.tpl

These are getting included on the parent template using

{include file="{$temp_folder}/{$inner_template}"}

So far great. What I wanted to do is having a default for, in the case that the file {$temp_folder}/{$inner_template} does not exists, it uses the equivalent file at default/{$inner_template}.

i.e. If I do {include file="place1/inner_c.tpl"}, since that file does not exists it in fact includes "default/inner_c.tpl"

Is it possible?

  • 写回答

1条回答 默认 最新

  • dsgni26260 2011-02-04 22:37
    关注

    You'll have to do it in php, smarty doesn't have a way to check if a file exists.

    You could write your own template handler too.

    <?php
    // put this function somewhere in your application
    
    function make_template ($resource_type, $resource_name, &$template_source, &$template_timestamp,
    &$smarty_obj)
    {
        if( $resource_type == 'file' ) {
            if ( ! is_readable ( $resource_name )) {
                // create the template file, return contents.
                $template_source = "This is a new template.";
                require_once SMARTY_CORE_DIR . 'core.write_file.php';
                smarty_core_write_file( array( 'filename'=>$smarty_obj->template_dir . DIRECTORY_SEPARATOR . $resource_name, 'contents'=>$template_source ), $smarty_obj ); 
                return true;
            }
        } else {
            // not a file
            return false;
        }
    }
    
    // set the default handler
    $smarty->default_template_handler_func = 'make_template';
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式