Love Fuel?
Donate
About
Forums
Discussions
Login
FuelPHP Forums
Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
General
Render an asset svg file
efx
April 2018
Hey Folks,
Is a way to render exactely like does Asset::js with false but for svg:
Asset::js(
'logo.svg'
),
array
(),
'svg'
,
false
); :
output:
<svg version="1.1" id="black_1_" xmlns="
http://www.w3.org/2000/svg
" xmlns:xlink="
http://www.w3.org/1999/xlink
" x="0px" y="0px"
viewBo ...
thanks
Harro
April 2018
Accepted Answer
Asset::instance()->add_type('svg', $rootpath-to-your-asset, function($images, $attr, $inline) {
// your code to render the asset goes here...
});
You could have a look at the Asset_Instance class, render_img(), to see how that is done.
efx
April 2018
So I have just to extends Asset class with the new asset type, I look that.
Thanks Harro
Harro
April 2018
Accepted Answer
That works too.
efx
April 2018
Hey Harro, on a basic test code:
\Asset::instance()->add_type('svg', '/themes/josselin/img/', function($images, $attr, $inline) {
$result = '<svg></svg>';
return $result;
});
echo \Asset::svg('logo-inline.svg', array(), 'svg', false);
or echo \Asset::svg('logo-inline.svg');
It return error: Call to undefined method Fuel\Core\Asset::svg()
it was not supposed to create a Asset svg instance?
efx
April 2018
no wrorry I just need file_get_contents, I will setup on my path's theme
thanks
Harro
April 2018
No, it doesn't have a static interface, as it's created on the instance, not globally.
\Asset::instance->svg('logo-inline.svg');
should work.
Do you still have your Asset class extension? If so, you could add:
/**
* Provide a static interface to the default instance methods
*
*
@param
$method
*
@param
$args
*
@return
mixed
*
@throws
\ErrorException
*/
public function __callStatic($method, $args)
{
return call_user_func_array(array(static::instance(), $method), $args);
}
Can you test this for me? If that works, I can add it to the core class...
efx
April 2018
I added code to file: core/classes/asset/instance.php after construct
and when I do:
echo \Asset::svg('/themes/josselin/img/logo-inline.svg');
It return:
The magic method __callStatic() must have public visibility and be static
I tried to replace with: public static function, no way, maybe I am wrong
Harro
April 2018
No, add it to core/classes/asset.php, that provides the static interface.
efx
April 2018
yeah it works but with 'public static function' instead of '
public function'.
Now I need to understand how return the content of my SVG file code on the page in this instance:
\Asset::instance()->add_type('svg', '/themes/josselin/img/', function($images, $attr, $inline) {
$result = file_get_contents($images);
return $result;
});
echo \Asset::svg('logo-inline.svg');
output: "Object of class Asset_Instance could not be converted to string"
I dont understand the path, the file is in:
http://josselin2.dev/themes/josselin/img/logo-inline.svg
if its works it would like nice to calll asset through Theme like with css, js.
:)
Harro
April 2018
Yeah, it should be a static method, sorry.
I don't see how given the examples the Asset_Instance() object could be returned?
And your closure should return HTML, so the output in your initial question, and not a dump of an svg file.
efx
April 2018
in asset instance js has this code:
protected function render_js($file, $attr, $inline) {
...
if ($inline)
{
$result = html_tag('script', $attr, PHP_EOL.$file.PHP_EOL).PHP_EOL;
}
return $result;
and with true $inline it return dump of js in html view
I could not have the same beviahor? sort of:
\Asset::instance()->add_type('svg', '/themes/josselin/img/', function($file, $attr, $inline) {
result = html_tag('svg', $attr, PHP_EOL.$file.PHP_EOL).PHP_EOL;
return $result;
});
echo \Asset::svg('
http://josselin2.dev/themes/josselin/img/logo-inline.svg'
);
Harro
April 2018
Accepted Answer
Added the required code to the Asset class:
https://github.com/fuel/core/commit/cc6096991c0811c75ea46f08a9762a5a4c40739d
Tested with:
\Asset::add_type('svg', 'assets/svg/', function($file, $attr, $inline) {
// return the svg file
return file_get_contents($file);
});
echo \Asset::svg('test.svg');
which assumes you have test.svg in ./public/assets/svg.
efx
April 2018
awesome Harro !
Add a Comment
Howdy, Stranger!
It looks like you're new here. If you want to get involved, click one of these buttons!
Sign In
Apply for Membership
Categories
All Discussions
5,089
General
↳ General
3,364
↳ Job Board
13
↳ Installation & Setup
214
Packages
↳ Oil
213
↳ Orm
700
↳ Auth
261
Development
↳ Tips and Tutorials
126
↳ Code share
145
↳ Applications
52
In this Discussion
efx
April 2018
Harro
April 2018