Stolz / Assets

An ultra-simple-to-use assets management library for PHP

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Optional attributes

SasSam opened this issue · comments

Hi,

Could you please implement an option to add attributes when you define an asset?
For example:

Assets::addCss('site.css', array('media' => 'screen'));
Assets::addCss('site-print.css', array('media' => 'print'));

And the generated will looks like this:

<link href="site.css" type="text/css" rel="stylesheet" media="screen">
<link href="site-print.css" type="text/css" rel="stylesheet" media="print">

Thank you!

I've posted similliar request, it's implemented already.

See #67

Sorry, I haven't seen it, but now I've just tried and the result is not really good. I work with Laravel 5 and in the Controller.php's constructor method I write this:

Assets::add('app.js')->js(['async']);

Assets::add('app.css')->css(['media' => 'screen']);
Assets::add('app-print.css')->css(['media' => 'print']);

But the generated result is this:

<link href="css/app.css" type="text/css" rel="stylesheet">
<link href="css/app-print.css" type="text/css" rel="stylesheet">

<script src="js/app.js" type="text/javascript"></script>

Any suggestion? I use the latest Assets (0.1.2).

commented

Both js() and css() functions are meant to be used in your views, not in your controllers. If not all your assets require the same tag attributes you will need to implement your own logic to determine the attributes depending on the asset.

/* In your view */

echo Assets::js(['async']);

echo Assets::css(function ($assets) {
    $output = '';
    foreach($assets as $asset)
    {
        $attibutes = [
            'href' => $asset,
            'type' => 'text/css',
            'rel' => 'stylesheet',
            'media' => (ends_with($asset, '-print.css')) ? 'print' : 'screen'
        ];

        $attibutesHtml = Assets::buildTagAttributes($attibutes);

        $output .= '<link ' . $attibutesHtml . " />';

    }
    return $output
});
commented

Please note last update has introduced the multitenancy feature of the library, which it may help you in that scenario.