简体   繁体   中英

Laravel 4 using php to create dynamic blade commands

I have this blade view template

<title><?
$page = App::make('page');
echo $page->getTitle( );
?></title>
<?php 
$css = $page->getCssList('all');

foreach($css as $item){?>
    {{ HTML::style('<?=$item['loc'];?>', array('media' => '<?=$item['media'];?>'))}}    
<?}

$js = $page->getJsList('all');
foreach($js as $item){?>
    {{ HTML::script('<?=$item['loc'];?>')}} 
<?}?>

It does not work. What happens is it actually outputs the blade commands to the screen. If i cut and paste these blade command into the view then they are rendered correctly.

What is the problem? Is is not possible to create dynamic blade commands? How can I solve this problem?

You are using PHP inside of PHP.

Change this

{{ HTML::script('<?=$item['loc'];?>')}} 

to this

{{ HTML::script($item['loc']) }} 

and change

{{ HTML::style('<?=$item['loc'];?>', array('media' => '<?=$item['media'];?>'))}}

to

{{ HTML::style($item['loc'], array('media' => $item['media'])) }}

The curly braces are simply a shortcut for printing PHP code.

You can just write

{{ HTML::script($item['loc']) }} 

Which is functionally identical to

<?php echo HTML::script($item['loc']); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM