plates icon indicating copy to clipboard operation
plates copied to clipboard

How to properly use asset extension?

Open CodeUmair opened this issue 1 year ago • 2 comments

I am trying to use the asset extension, I have a public directory with index and htaccess and an assets directory, I am trying to load assets, CSS, js, and images from this directory and nothing is working for me.

$engine->loadExtension(new League\Plates\Extension\Asset(BASE_PATH . '/public/assets', true));

CodeUmair avatar Jun 13 '23 17:06 CodeUmair

Same here.

Sorry for my english... anyway:

This is my controller:

<?php

namespace src\controllers;

use League\Plates\Engine;
use League\Plates\Extension\Asset;

class Web
{
    private $templates;

    public function __construct()
    {
        $this->templates = new Engine(__DIR__ . "/../views", "php");
        $this->templates->loadExtension(new Asset(__DIR__ . '/../assets/', false)); // or true
    }

    public function home(): void
    {
        echo $this->templates->render("register-and-login/home", [
            "title" => "Home | " . SITE,
            "users" => ["Adeilson", "Karol"]
        ]);
    }
}

And this is in my template:

<link rel="stylesheet" href="<?= $this->asset('/css/style.css') ?>">

If I change to a non-existent path it gives an error 500, but if it is correct it defines the following path when rendering:

<link rel="stylesheet" href="/css/style.css?v=1687988335">

And this error:

GET https://mysite.com.br/css/style.css?v=1687988335 net::ERR_ABORTED 404 (Not Found)

Why? Someone can help me?

adeilsonaalima avatar Jun 28 '23 22:06 adeilsonaalima

<link rel="stylesheet" href="<?= $this->asset('/css/style.css') ?> should be <link rel="stylesheet" href="/assets<?= $this->asset('/css/style.css') ?> i think?

The asset extension doesn't return the full public path, so you still need to put that in the html, it you view the generated source and compare the generated url to the expected url you should see this - from your example the html does not include the public assets folder. I've created a pull request #318 to update the docs with the assets path.

RawkBob avatar Jul 05 '23 09:07 RawkBob