web2py controller for layout.html - html

I'm building a web application using web2py, and have currently implemented search on some of the view/ pages by creating a function in the appropriate controller and then using {{=form}} to render it in my HTML page like this...
<h3>Search</h3>
{{=form}}
{{if results:}}
<h3>Results</h3>
{{for result in results:}}
{{=A(result.title)}}
{{pass}}
Rather than repeat this functionality in multiple pages, I would like to have a search field in my layout.html file, which can then be used across all over views. However, there is no controller for layout.html. Is it possible to use a method in another controller in the layout.html page, or is there another way to link a controller to layout.html that I am missing?
Thanks.

Related

Template Inheritance doesnot work in Django

I have base.html (which is my home page). It has navbar which is in navbar.html and some sliders in base.html itself. I have created ProductList view which render context(i.e products), which is used in product_list.html to make values dynamic.
Now my problem is I don't want to include my ProductList view in my product.urls because i don't want it to be rendered in new page. Instead I want to include product_list.html in base.html inorder to show it in my home page rather than in product page just like in normal ecommerce site.
So, I didn't include ProductList in product.urls and included that template in base.html using include template-tag. But it didn't show my products. When i tried to render products in separate page it works.
Is there any method to do this?
I think you forget to pass the context.In your views.py, pass the context to the function which renders the base.html
For example:
def index(request):
context = {'product':products} #Pass your products
return render(request,'base.html',context)

Using templates for multiple pages in meteor

I was working on a web app prior to finding meteor and to have multiple pages I had things like this (I'm using bootstrap):
signup
In order to have a button for a user to sign up. I'm trying to convert this to a template in order to work with the meteor framework. I made a template that had all the code from my signUp.html file and changed that line of code to look like this:
signup
and this gave me the following error:
INCLUSION template tag is not allowed in an HTML attribute
I changed it again to be like this:
<a {{> signUp}} class="btn btn-lg btn-default">signup</a>
and I got this error:
Reactive HTML attributes must either have a constant
name or consist of a single {{helper}} providing a dictionary of names and
values. A template tag of type INCLUSION is not allowed here.
Any help would be appreciated.
What you really need is a router. Check out iron router
You can add it using
meteor add iron:router
Then, set up the routes for your signup page. (Assuming that you have named the template as "signUp" )
Router.route('/signup', function () {
this.render('signUp');
});
And finally use the link as :
signup

Where to keep the javascript code of a partial view (which can be included using #include) in blade templating?

I have a structure where a view.blade.php extends a layout.blade.php. Layout blade has #yield('js').The view page which extends layout blade needs to include a partial form which has some javascript. I have a confusion about where to keep the javascript code for the form so that it is yielded in the layout. Help appreciated. Thanks.
its really easy with laravel blade you can put javascript for partials blade file
when you will call them they will automatically included in layout.blade.php here is example of your all views.
layout.blade.php
#yield('js')
partial.blade.php
#section('js')
<script src="patial/jsfile.js"></script>
#stop
view.blade.pp
#extends("layout")
#include("partial")
this will work like charm and really simple.

Do I need a controller for any routes in Rails?

I am implementing an html template for a Contact us page in my app. I am wondering if I can somehow pass a link for that page, which will have no behaviour, without having to generate a new controller which seems to be not possible. I tried to place the template in the public folder and then pass a link to /public/contact, being the template contact.html.erb, but rails returns a uninitialised controller routing error.
here is what I have set in the routes.rb file
get 'public/contact'
and this is the application.html.erb that carries my link to that
</div>
<ul>
<li>HOME</li>
<li>NEWS</li>
<li>FAQ</li>
<li>CONTACT US</li>
</ul>
Notice that we have also the path info/news and info/faq
I had generated a controller info to handle all these static pages. I would just like to know if this a good practice or should I use other drier way to implement this?
It is actually a good approach to have a controller for your static pages as well, you can actually generate a controller for pages to handle the static pages like about, contact e.t.c rails generate controller pages contact about
you would just access that in
localhost:3000/pages/contact
Then your view would be in app/views/pages/contact.html.erb
routes.rb
get "pages/contact"
get "pages/about"
wHY do you need to generate a new controller just for a request.Recommended way is to just add a new action in your common/utility controller with a get request to show about us.
for example: match '/about' => 'pages#about'
This is good as you can cache the page to boost performance as well you can make it dynamic(html.erb)
OR
Assuming that the static file(only html) is in public folder,ADD an anchor tag directly to open it in new page using target=_blank attribute.
<%= link_to "About Us", "/about_us.html" ,:target=>"_blank"%>

Include plain HTML page inside a Play Framework view template

Is there a way to include a plain html page inside a Play Framework's view template? I have a scenario wherein there is a common view template and in the body of the template, I would like to include certain static html pages. I know that I can include other templates inside a certain template, but I'm not sure if I could include a plain html page?
One option is to just make your static HTML a template, eg, create myStaticPage.scala.html:
<h1>Static page</h1>
<p>This page is static, though it is still a template.</p>
Then your view template, myView.scala.html:
#(staticPage: Html)
<html>
<head>...</head>
<body>#staticPage</body>
</html>
And then in your action that renders the template:
def renderMyStaticPage = Action {
Ok(views.html.myView(views.html.myStaticPage())
}
You just need to make sure that your HTML page escapes any # symbols with ##.
On the other hand, if which HTML page that's being included is more dynamic, then simply load the HTML from the file/database/classloader/whereever it's coming from, eg:
def renderMyStaticPage = Action {
val staticPage: String = // code to load static page here
Ok(views.html.myView(Html(staticPage))
}
You could.
Just put something like that in your routes file:
GET /file controllers.Assets.at(path="/public", file="html/file.html")
Here is a duplicated post: Route to static file in Play! 2.0