Currently using the SlimPHP 3 MVC framework and trying to XML output of a URL to the Twig template. I'm using the latest build of Guzzle if it helps.
I have my Controller set up and the function as follows:
public function getaws($request, $response, $args) {
$bucketname = $args['bucketname'];
$client = new \GuzzleHttp\Client();
$bucket = $client->request('GET', $bucketname . '.s3.amazonaws.com');
if($bucket) {
$buck = $bucket->getBody();
return $this->view->render($response, 'aws.twig', ['buck' => $buck]);
}
else {
$res = Array();
$res['message'] = 'Bucket Disabled or Unknown';
return $this->view->render($response, 'aws.twig', ['buck' => $res]);
}
}
In the Twig file - aws.twig I have only been able to output the entire XML response/body using:
{{ buck }}
I have tried typing:
{{ buck.Name }}
or
{{ buck.Key }}
Neither have worked. I've tried the ForEach loop as well and nothing comes from that at all. I'm completely at a loss. I've noticed that there doesn't seem to be an awful lot of help on SlimPHP but I like it and prefer using it to Laravel and no I won't switch to Laravel just to make life easier otherwise asking for help with this would be a waste of time.
Anyone who has had experience with SlimPHP 3, Twig templates and Guzzle - I'd very much like help with this.
Thanks!
Related
I'm trying to show an Advanced Custom Fields repeater in custom order in a Timber/Twig based WordPress theme. Is it possible to install the array-extension (http://twig-extensions.readthedocs.io/en/latest/array.html) to achieve this or how can it be done? I'm completely lost at the moment and would appreciate any ideas on how to solve it.
I wouldn't use a twig extension.
Why not something like this:
$rows = get_field( 'repeater_field' );
if( $rows ) {
shuffle( $rows );
foreach( $rows as $row ) {
// your code
}
}
In your php file (index.php, page.php etc.)
// Create context
$context = Timber::get_context();
// Get Field
$repeat = get_field('my_repeater_field');
// Shuffle it
shuffle($repeat);
// Add it to context
$context['repeat'] = $repeat;
And then call it in your twig file as your normally would
Using Timber, you can also do this with the Twig sort filter:
{% for item in items|sort(rand) %}
https://twig.symfony.com/doc/2.x/filters/sort.html
I just want to display a Excel file existing on my hardisk in a twig view.
I'm using Symfony[2.5] and "Liuggio/Excelbundle"
This trick works well but i want to add it in my view.
public function newAction()
{
$filename = 'filename.xlsx';
$reader = \PHPExcel_IOFactory::createReaderForFile($filename);
$excel = $reader->load($filename);
$writer = \PHPExcel_IOFactory::createWriter($excel, "HTML");
$writer->generateStyles();
$writer->generateSheetData();
// this doesnt work..
return $this->render('MonextReportingBundle:Default:excel.html.twig', array(
'excelHtml'=>$writer
));
And in my excel.html.twig :
{{ excelHtml | raw }}
Catchable fatal error: Object of class PHPExcel_Writer_HTML could not be converted to string
Thanks a lot guys ! Excuse my english..
PHPExcel's HTML writer has no toString(), that's why your attempt didn't work.
It has, however, a method called generateSheetData that seems to do what you want. Use it like this:
{{ excelHtml.generateSheetData | raw }}
I am using the silex framework for my project. I am using the SecurityServiceProvider with a custom user implementation. The login/logout works and I am able to view the correct user information in the symfony profiler (stored as a session attribute). Now I am trying to add the custom user information to the twig environment so that is is accessible from the templates. Here is what I've come up with:
$app['twig'] = $app->share($app->extend('twig', function($twig, $app) {
$token = $app['security']->getToken();
$userInfo = null;
if (null !== $token) {
$userInfo = $token->getUser()->getTwigInfo();
}
$twig->addGlobal('userinfo', $userInfo);
return $twig;
}));
I am trying to extend the environment and it works, however the user information seems to be processed later and my userinfo attribute is always null. I guess that I have to somehow extend the twig environment later but do not know exactly how to do that. Can someone help me?
Silex gives you access to the app instance directly from Twig.
So you could do:
{{ app.security.token ? app.security.token.user.twigInfo : null }}
or
{% set userinfo = app.security.token ? app.security.token.user.twigInfo : null %}
{{ userinfo }}
If you prefer to handle it within your PHP code, then you can create a new definition
$app['userinfo'] = $app->share(function($app) {
$token = $app['security']->getToken();
return (null !== $token) ? $token->getUser()->getTwigInfo() : null;
}));
Then in your Twig template
{{ app.userinfo }}
Would anyone please advise how in jade for nodejs I can truncate a string to a number of characters/words, ideally conscious about the HTML markup within the string?
This should be similar to Django's truncatechars/truncatewords and truncatechars_html/truncatewords_html filters.
If this doesn't exist in jade, which way is right to go? I'm starting my first nodejs+express+CouchDB app, and could do it within nodejs code but it seems that filters are much more appropriate.
I would also consider writing a filter like this (and others) if I knew how :))
Just a quick illustration:
// in nodejs:
// body variable comes from CouchDB
res.render('home.jade', { title : "test", featuredNews : eval(body)});
// in home.jade template:
ul.thumbnails
each article in featuredNews.rows
a(href="#"+article.slug)
li.span4
div.value.thumbnail
img(align='left',src='http://example.com/image.png')
p!= article.value.description:truncatewords_html(30)
So I've made up the truncatewords_html(30) thing to illustrate what I think it should be similar to.
Will appreciate any ideas!
Thanks,
Igor
Here is a little "truncate_words" function:
function truncate( value, arg ) {
var value_arr = value.split( ' ' );
if( arg < value_arr.length ) {
value = value_arr.slice( 0, arg ).join( ' ' );
}
return value;
}
You can use it before sending the string to the template, or in the template using a helper method.
cheerio is a nice little library that does a subset of jquery and jsdom. Then it's easy:
app.helpers({
truncateWords_html : function(html, words){
return cheerio(html).text().split(/\s/).slice(0, words).join(" ")
}
})
Then, in a jade template use:
#{truncateWords_html(article.value.description, 30)}
This looks like a generic way to add any filters, hurray! :))
I'm learning Kohana 3.2.0 together with KSmarty for Kohana 3. I'd like to write an anchor on the page like this:
Page list
I can build the url in the controller and pass it to Smarty as a variable but. Is there a way to build the anchor or URL in Smarty template (including "http://www.mysite.cz" part)?
If it is not possible to build the anchor. Is it at least possible to build full URL?
The Reason: I have a main template which includes another template.
The main template will be used by multiple controllers and I would like to avoid building the URL in each controller. Therefore I'll be happy if KSmarty will be able to do it for me.
The only solution I have found is to write custom function. Save following code into function.url.php file in Smarty plugins directory:
function smarty_function_url($params, &$smarty)
{
$type = '';
if(isset($params['type'])) $type = $params['type'];
$protocol = 'http';
if(isset($params['protocol'])) $protocol = $params['protocol'];
$url = '';
if(isset($params['url'])) $url = $params['url'];
$text = '';
if(isset($params['text'])) $text = $params['text'];
switch($params['type'])
{
case 'url':
return Kohana_URL::site($url, $protocol);
case 'anchor':
$url = Kohana_URL::site($url, $protocol);
return "<a href='{$url}'>{$text}</a>";
default:
return Kohana_URL::base('http');
}
}
Examples of use in Smarty template:
{url}
{url type='url' url='admin/categories' protocol='https'}
{url type='anchor' url='admin/articles' text='List of articles'}
The first block in which variables are set I had to write otherwise Smarty was generating notice "Undefined variable...". I'm just PHP student, suggestions for code improvement are welcome.
Hope it will help the others.