beginner pylons / mako templating questions - pylons

I've been reading mako / pylons documentation and am having trouble finding good examples / discussion of integrating multiple applications into a single page, so for example, if i had a blog application and an application which just selects a random quotation from a database of awesome quotations. (the example is trivial, just for exposition)
So I want to make a single page which displays both applications,
I'm not sure how to go about writing the templates, i've seen next.body(), do I need to call the controller from the application in a template?
How could I do this on the fly?
Can I just make a model describing the page, and have each application it wants to call as objets in the attributes of the model?
I'm just spitballing here, hoping for a pointer to an example / tutorial i can look over.
Thanks for any advice.

In Django, "applications" are components you develop or choose, then customize, mix and match together to get your final website.
In Pylons, "application" refers to the whole thing you're creating--controllers, models, templates, middlewares, any helper modules, documentation etc. A Pylons application is a bunch of code that can be served as-is by Paster, Apache/mod_wsgi or any other WSGI-supporting web server.
In Pylons, to get, for example, blog posts and random quotations in single page, you'd:
Create separate models for representing and manipulating blog entries and quotes, like myapp.model.Post, myapp.model.Quotation.
Create a controller action that queries models to get X blog posts and a single quotation, put them in data object that will be passed to template.
Create a template that displays both blog posts and quotation on single page. This template can, of course, inherit from some base template or include template fragments, so you don't have to copy-paste common HTML code across many templates.
Hope this helps!

Related

What's the difference between controllers and actions in ruby on rails?

Can anybody tell me the difference between controllers and actions in ruby on rails?
I fetched this definition from the official rails guide:
A controller's purpose is to receive specific requests for the application. Routing decides
which controller receives which requests. Often, there is more than one route to each
controller, and different routes can be served by different actions. Each action's purpose is
to collect information to provide it to a view.
I am confused.
Please, make it as simple as possible since I am newbie!
Thanks!
Controllers are just Ruby Class files which have a series of instance methods inside
Basic Explanation
Rails controllers are basically files where actions (methods) are kept
Each time you access a Rails app, you're sending a request to the system. The various technologies inside Rails route that request to a certain action, where your code can use the passed data to perform some sort of action (hence the name). The actions are kept inside controllers to give the application structure
So if you access http://yourapp.com/users/new, it tells Rails to load the new method in the users controller. You can have as many actions in the controllers as you want, but you have to tell the Rails routes system they are there, otherwise they won't be accessible
Proper Explanation
Rails Controllers are just Ruby Classes, storing a series of actions
The "actions" (instance methods) work on passed data (params) to create objects that can either be passed to the model, or used inside other methods
Whenever you send a request to Rails (access a URL), it first uses the ActionDispatch middleware to send your request to the correct Class (controller) instance method (action), and then your code does something with that data
Your job as a dev is to connect the right controllers with the right models, presenting the right data the user at the right time
DISCLAIMER: I don't write code in Rails (never did). I write Sinatra modular applications and use the MVC model.
You first need to clarify the MVC model. The MVC is an approach to programming web applications (in RoR) or user interfaces in general. So MVC stands for Model-View-Controller. I will try to explain a bit, but in order to understand this, you need to practice and play with it.
The Model: If you remove the layers of abstraction, it's your database scheme. The way your application interconnects in order to retrieve information.
The View: The way these informations are retrieved elaborated and served. Essentially is what you, or the client, see in the browser.
The Controller: The controller is what interacts with the program to produce a requested view or to alter a model. You request a view when you access a chart with statistical information, and you alter the model when you input DATA on it. In Rails ecosystem, ActionController is a class with a set of predefined methods to help you perform easier and quicker standard Controller actions like update a form, etc.
So the Action Controller allows you to alter data to your models (the db), or request a route to view your data, etc.
Action is not separated from controllers, it's basically what controllers do :-). Everything else is static.
If you feel that these concepts are still hard to grasp, try building a very basic modular application in Sinatra, and you will have a ground level view of how things work.
Explanation by Analogy (simple explanation without getting too technical)
I work in a busy office. I bark out orders (i.e. 'requests') to my staff to get em to do stuff.
e.g.
Sometimes I want a document so I can read it.
“Ngozi, pass me the ABC.ASX EOFY results please?”
Yes sir!
Sometimes I ask my staff to edit an existing document:
“Sunita, can you edit that report on the state of the union address?”
“Sure!” is the response.
I organise my staff based on the type of work they do
But I have a little problem.....I have 10,000s of different types of documents. Sometimes I want to get: (I) sports results and other times I want: (ii) the evening news, while still at other times I want: (iii) a collection of Donald Trump's latest 4 am Tweets.
So I created a new system. I have a staff member directly responsible for each type of thing.
Ngozi handles ASX (Australian Stock Exchange) Financial Results. And when I want Ngozi to do something (i.e. perform some type of action) then I tell him what to do.
Sunita works mainly on politics. Sometimes I”ll ask her to something (e.g. write up a report – this is one type of 'action', or I'll ask her to bring me a certain document – another type of action - and she'll do it. I like to get Sunita to work on politics and Ngozi to work on financial results. It's best to keep their responsibilities separated.).
And Freddie works on anything pertaining to Queen.
Etc. etc.
The meaning of the analogy?
In this case, the controller would be the person – who's responsible for handling certain types of requests. And the “action” would be the particular specific thing that I want done:
e.g.
getting a document or
edit something or even
creating a new document.
Hope that clears things up.

Sharing Orchard CMS blog posts - overriding templates

I'm using Orchard CMS and want to be able to show sharing links (think AddThis.com or Shareaholic.com) to blog posts on a website.
Before I start I realise there is a similar modules already present in the Orchard gallery but a, I couldn't get the module to work and b, I'd like to further my understanding of Orchard.
My thought originally was simply to add a new blog widget layer and to add a new HTML widget that contains the aforementioned sharing code. Trouble with this approach was;
The layer rule would force all of my blog pages to have to be under some common url i.e. using a layer rule of something like the following. Not necessarily a bad thing but something that could be missed when the client publishes a new post.
url("~/blog*")
The sharing code contains empty html anchor elements that were getting stripped from the html when I saved in the Orchard editor.
I think #2 is down to the template used in my theme. I read a post from Bertrand Le Roy's blog about overriding templates which got me thinking. Can I just override the Orchard blog template and place the sharing code in there directly? Clearly this is less customisable in the future but I'm happy with that. I'm also thinking having fewer layers, moduels, widgets or whatever can't be a bad thing when considering Orchard performance and load times. I'd also now have my sharing icons on every blog post wherever it may be on the site.
Question is what is the view that I should override in this case? I tried editing the blog views in Modules\Orchard.Blogs\Views expecting to see some changes but I got nothing.
What would people suggest as the best way forward?
The best approach IMHO is to add the code in one of two (or both) templates:
Content-BlogPost.Summary.cshtml when the blog post is shown in a list (you can copy the code from /Core/Contents/Views/Content.Summary.cshtml) and
Content-BlogPost.Detail.cshtml when the blog post is shown in a separate page (you can copy the code from /Core/Contents/Views/Content.cshtml)

Structuring a Rails CMS type application

I've just finished going through my first book learning RoR and wanted to practice some more extending the project, but I'm at a bit of a loss for how to structure the additions I want to make and would appreciate some guidance.
Right now the application is separated by pages that have a model controlling the logic and a controller routing that logic to the view, but none of the pages have any cross-over in features. I want to create a page that has pre-templated features with logic from other models, sort of like widgets in WordPress, or plugins in Magento. If I had a Page model and wanted to inject a Bestsellers list in the view, or I had a Blog model and wanted to inject a list of products with a tag calling out to a template with all the markup already, what is the proper way to do this?
Would these have to be modules? Would I just create another view template for Catalog that I would call into the Page index view?
Nevermind, I found the answer to what I was looking for here

Orchard CMS and controller vs driver

I am a little ashamed for asking so many questions, but I really want to learn.
In Sipke's blog a webshop is created. There is one specific question that boggles my mind when trying to do something similar.
Let me spell out the basic requirements:
User registration form and login, etc. This one is covered by the blog and it works nice.
Creating product parts and so on. This one is covered and no problem there.
Ordering by filling in an order form and making the payment. See down
Having the order page maintainable by customer. See down.
Viewing your own orders and their status. See down
Maintaining customers and orders from backend system. This one is covered by the blog and I need to do some work there yet.
As for items regarding creating orders and viewing your orders. I have followed the approach for creating records and using standard MVC controllers. But then I encountered problems:
Menu for orders page. This I had to do manually after installing the module.
The order page itself. I had to create the view including title and so on. But I can imagine a customer wanting the order page on another menu and with a different title. And maybe add even some own content to the ordering page. This I couldn't achieve by using standard MVC approach. So maybe I am using the wrong approach here. So I was thinking about using contentparts for creating an order and displaying them and using the drivers and handlers for that. But before I go down that road and refactor everything I want to know if that is the right approach. A downside could be that once the module follows that route it can then not so easily be reused with customers that have other cms's capable of hosting an MVC3 module.
So when to use drivers, handlers and contentparts and when to use standard controllers and views.
You should use Drivers and Parts (with Handlers if needed) when you want to create functionality for content items. E.g. if you want to display a custom media with all products, you could create a Part (together with its Driver, etc.) to handle that. Read the docs on Parts.
If the functionality is not tied to content items the most possibly you want to use the standard MVC toolbox, it's fine. Look at the built-in modules how they do that. E.g. the Blog module uses controllers and views to show the admin UI, but has parts to enhance the functionality of for example the Blog content type.
To make things more complicated you can employ ad-hoc content items to build a page that you'd normally do with simple views, but that's an advanced topic :-).

How do you write shared generic views with the new MVC 2

About a week ago I saw a blog post describing how you could write a set of shared Views in the Views/Shared folder that could be used by all your Models. The post also explained how to decorate the Model properties with validation rules and messages. There was information about the new Model Binding improvements. The examples take advantage of new functionality in the preview release of ASP.Net MVC 2.
For the life of me, I can't find the blog post or other information on how to create the shared views.
Can anyone offer a pointer to the post?
Ask and you shall receive.
I've been playing with this quite a bit in the past few days. In addition to generic, dynamic display of editors, details, etc., I've also implemented generic, dynamic modification of data. Essentially, Phil's sample project covers the GET side of things, and I've implemented the POST. I'm also looking into dynamically generating JavaScript in order to have a dynamic list using jqGrid. Stay tuned to my blog if you're interested in this sort of thing.

Resources