I'm working inside Fat-Free-Framework, using the F3 Template Language, but I've come across an issue I'm not sure how to solve.
I've got a portion of a webpage, which contains data from a database that needs to be updated at some interval to make sure the person looking at the page is looking at up-to-date information.
I can easily load the data this way:
<div id="container">
<include href="module/views/table_pending.htm"/>
</div>
This works, but it doesn't update at an interval. I know I need to use jQuery for this, so I tried to use the load() method to load this page fraction, but it isn't rendering properly.
This is my Javascript
function loadPageContents() {
$('#container').load('{{#BASE}}/app/modules/module/views/table_pending.htm');
}
loadPageContents();
Here is a minimal version of my table_pending.htm:
<repeat group="{{ #data }}" value="{{ #device }}">
{{ #device[uid] }}<br>
</repeat>
When using F3's include element, my page renders as expected:
But when using jQuery .load(), it seems that F3's template language isn't really parsed at all (thus variables not being replaced, loop not working properly etc):
So my question is, how can I include this page fragment into my main page, update it on an interval, and still render my template language?
Related
I am trying to make an #if-statement to only display a certain jQuery DataTable function if a certain Blade file has been #included into a show blade.
This might sound like making it harder than it is, but I want to keep the #section('scripts') area clean if it is not necessary to use it within the current show-page.
Placing the <script> within the #include did NOT work;
Placing the <script> within the show-page did work, but makes the show-page bloated; and
The current code (see below), only checks if the view exists so it will always be TRUE.
#if ( View::exists('admin.users') ) {{-- on the current Blade! --}}
$('#usersTable').DataTable({
"language": #json(__('datatables')),
});
#endif
I've encountered a similar issue myself. To solve it you can make use of blade stacks.
The way I do it is in the footer of my main layout, below the script tags including the js I put #stack('javascript'). Then, in a view where I want to include some js, so for you this would be in the admin.users view I would add..
#push('javascript')
<script>
// my js here
</script>
#endpush
That way the js will be appended to the footer once the view is included.
For more information see the docs.
Prior to vue being part of Laravel, I would often debug by simply using {{ dump($var) }}
Doing so now throws an exception within javascript as Vue doesn't like <script> tags within the body of the content. It's not game breaking, but it is annoying. It still shows the whole array, just that the JavaScript around it doesn't work anymore.
Is there a Vue equivalent of laravel's dump? Can I tell Vue to ignore script tags when doing debugging?
To be clear, I'm looking to output the variable from PHP where it shows the properties, attributes, the type of value etc. I'm not looking to output an object within JavaScript.
console.log() will work but it will work only via javascript but i personally suggest that you will use vue.js plugin from browser? its way better to understand what is really happening on your vue variables in the back.
heres the link for
vue dev tools chrome
then on your browser(F12) u will find it from Vue tab together console,network html.
You just need to have the dump() outside the div your vue app is mounted to. So for example in your layout.blade
<body>
#yield('dump')
<div id="app">
Then in your index.blade
#extends('layout')
#section('dump')
#dump($variable)
#endsection
You can try {{ var_dump($variable) }}
If you are just looking to see the contents of the variables you are working with you can use the PHP functions var_dump() instead of dump(). It's not styled in the same sleek way that dump() is and you can't collapse or expand sections like you can with dump(). But it will dump the data you need without messing with your Vue code.
{{ dump($variable) }} without Vue:
{{ var_dump($variable) }} with or without Vue:
I know this is very basic and I've never had the need before, but I want a PHP redirect to break out of any iframe (if applicable).
Is this possible?
I know that PHP sends a header to the browser, so it should be, but I can't see this anywhere.
So if I use this code then it always changes the parent and not just any iframe window:
<?php
header('Location: http://domain.com/');
?>
Of course this works, but I need it done from PHP:
<a href="page.php" target="_parent">
I'm using Symfony2 so will need to get it working in that too.
No JavaScript. PHP only (if possible).
You can't do it in PHP - if the browser is trying to load the page in a frame, then sending a Location header will simply load the new page in the frame too.
If you're trying to stop others from putting your site in a frame, you'd have to use JavaScript to detect it and do the redirect, otherwise target="_top" is the right approach.
From your controller in Symfony 2, return some variable (for example reloadMyIframe) then, when you want to reload your iframe. Then in view (twig probably), put something like this
{% if reloadMyIframe == true %}
// Add somethin like this <script...>window.parent.location.href = "http://jourTaret.dmn"
{% endif %}
If your view in Symfony is in PHP, you can use same logic just type it with php if statement.
As the guys have already said, there is no way to accomplish what you are trying to just with PHP or from Symfony controller.
I'm new to Symfony2 and I've been thinking of the best way to generate navigation bar HTML that is used on every page request - especially with regards to caching.
So imagine if every page request shows the logged in user and a number indicating how many messages are unread (in fact like stackoverflow for that). I'm guessing that could be generated in every controller to ensure the info is up-to-date (using a function or something of course) - but I'm also looking at caching the whole controller output, and think it might be good to keep this dynamic part separate.
Would creating a controller extension for all this kind of stuff be a good way to go? So this way the controller only deals with that specific function (e.g. getting blog posts from a DB etc.) and the controller extension adds all the dynamic content. That way I can cache the controller result and speed up pages without caching the full page (which can't really be done due to lots of dynamic HTML content).
Something like this maybe:
class ControllerExtension extends Controller
{
public function render($view, array $parameters = array(), Response $response = null)
{
//get number of messages for this user
$parameters['messages'] =
//are they logged in
$parameters['logged_in'] =
// render as normal
return parent::render($view, $parameters, $response);
}
}
For this I want to ignore use of JS. I know some of these things could be populated with JS, but I would prefer not for this.
You can solve this by caching the navbar fragment separably from the page html with ESI or Hinclude and can be simply and elegantly solved with Symfony2.
Embed a controller inside a template
You can render a controller inside a template:
<div id="sidebar">
{% render url('latest_articles', { 'max': 3 }) %}
</div>
This will render the controller with the route "latest_articles", inside your html.
This can be done in you controller template, or in the global layout template (where you define the header, footer, js, css ecc of all your pages, see Template Inheritance)
Cache the embedded fragment separately from the page html:
You can use a reverse proxy (like Varnish, or the AppCache), to cache the two part of the html separately:
<div id="sidebar">
{% render url('latest_articles', { 'max': 3 }, {'standalone': true}) %}
</div>
That's it, just add {'standalone': true}
You'll need an external program on front of your web server (like Varnish, or Nginx with a mod), but this is the fastest way.
Load the fragment with javascript:
You can also tell symfony to asynchronously load the fragment in javascript:
<div id="sidebar">
{% render url('latest_articles', { 'max': 3 }, {'standalone': 'js'}) %}
</div>
This is a nice approach, since you can cache the entire html in a CDN (for example with Amazon CDN CloudFront), and still show user specific content.
For info see: http://symfony.com/doc/2.1/book/templating.html#asynchronous-content-with-hinclude-js
It seems F3 framework doesn't handle php function calls within a page? I have a php navigation bar, which is uniform site-wide. I call up my layout page in my controller class thus: Template::serve('layout.php'). In the layout page, I include the navigation bar thus: <F3:include href="navbar.php" />. Within the navbar (navigation) file, I call a utility function siteUrl which gets the absolute url to a resource e.g. css or .js file. This function is defined in an include file which I include as follows: require_once "lib/globals.php. Within the navbar.php, I use the siteUrl as follows for example:
<img id="logo" alt="logo" src="<?php echo siteUrl('small-logo.png') ?>" />
This doesn't seem to work. When I view the generated source of the page, the src section of the img tag is an empty string: "". However, when I call the navigation bar from other pages that are not using the F3 framework (i.e. pages that are not being routed by F3::route. Not all pages of the website are routed using F3), it works fine.
What could be the problem? How could I call a php function from within a php page that is being rendered using Template::serve? It seems the entire content between the <?php ?> tag is not being executed when the page is being served by F3. Echo statements are not being displayed. Thanks for responses.
Template::serve() does not allow PHP. It is a templating engine. There are things you can do. You can define a function using F3::set('sum',function($a,$b){return 1+2;}); and then reference that function in the template with {{#sum(1,2)}}. I would re-read the templating documentation on the fatfree site: http://bcosca.github.com/fatfree/#views-templates
Again, the reason PHP is not working is because you are using Template::serve() and are therefore using the templating features of Fatfree. If you want to use PHP, I believe you can use F3::render() instead and it will render the page, allowing PHP, but you will lose all the templating functionality.
you can use raw php within the template tokens wrapped by curly brakets like this:
<img id="logo" alt="logo" src="{{ siteUrl('small-logo.png') }}" />
it will echo it automatically.
but using F3::set('image.smallLogo',siteUrl('small-logo.png')) to define the image paths and grap them with a simple {{#image.smallLogo}} feels much better.
page moved:
Fat-Free Framework 3 Template Directives