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 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图