dongluan1901 2013-09-28 13:48
浏览 9
已采纳

将常规PHP格式化为智能代码

I am familiar with php but not familiar with smarty template code.

I am trying to set a value that is set by my script that is formatted like this {$p.youtube_key} it's a variable that will output the given url when a user has set a video as their upload post.

So in smarty, {$p.youtube_key} will display as their given url that they submitted.

As for php, I have a html5 player that I need to output the video link in.

Here's my code:

<video width="800" height="680" style="margin-left:-30px;"
 id="post_html5_api" autoplay="true" class="vjs-tech"
 loop="" poster="" type="video/mp4" preload="auto"
 src="<?php echo yt_video('baseurl'); ?>" >

The src= is where I need to output {$p.youtube_key}. But, I have no idea how it should be setup in the smarty template.

  • 写回答

1条回答 默认 最新

  • dtxpz8785 2013-09-29 03:05
    关注

    I'm not quite sure what you don't know how to do, so I'm going to guess a bit; apologies if I've misunderstood what you're asking.

    To include the URL in the Smarty template, you use the syntax you've already posted; in place in your HTML, it will look like this:

    <video width="800" height="680" style="margin-left:-30px;"
     id="post_html5_api" autoplay="true" class="vjs-tech"
     loop="" poster="" type="video/mp4" preload="auto"
     src="{$p.youtube_key}" >
    

    The {$p.youtube_key} is essentially the same as saying <?php echo $p['youtube_key']; ?>, so you'll need to set that variable in the PHP code that feeds the template. That part will look something like this:

     $post = array(
          // ...
          'youtube_key' => yt_video('baseurl')
     );
     $smarty->assign('p', $post);
    

    A few other tips:

    • You can wrap the whole video widget in {if $p.youtube_key} ... {/if} to hide it if there is no video to show (just like you would in plain PHP).
    • Rather than assigning the result of yt_video to a variable, you could register a plugin, which is just a PHP function which you've told Smarty how it should be used in a template. So you could have something like {get_yt_video_widget video_id=$p.youtube_id attr_autoplay=true}, which worked out the full URL, set the type attribute, etc.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?