将常规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.

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.