开发者

Adding content to static files(pages)

I have several static files(pages), which are basically copies of my website pages source code, with the content changed.

These files support my website, (keeping the same 开发者_如何学运维format) in various ways.

For example the menu part is:-

<body>

<div id="menu">

<ul class="level1" id="root">
etc
etc. until
</ul>
    </div>

Unfortunately every month or so my menu bar changes and I have to update each static file manually. As each of my static files have the same menu.

Is it possible to have one menu file which can be updated and have the static files load them automatically.

I plan to have several more static files. So this would be a great help if someone can suggest how to accomplish this.


Oh yes. Use some javascript magic to load the menu bar upon page load and keep it in menu.html.


One solution may be to use a spider (wget --recursive) to download generated pages directly from your application. One command, and you have the full copy of your site. (just add some useful options, like --convert-links, for example).

The other option may be to write an after_filter in your controller, and write the generated content to a file (not always, but for example when you add a parameter ?refresh_copy=1). Maybe just turning on page caching would be suitable? But the problem will be that you will not be able to trigger the controller action so easily.

If you don't want the whole site copied, just add some specific routes or controllers (/mirrorable/...) and run the spider on them, or just access them manually (to trigger saving the content in the files).


I ended up creating one controller without a model.

 rails g controller staticpages

I then created a layout file which imported the individual changes to the layout, via a "yield" tied to a "content_for" in the view files(static files(pages) in the "view of staticpages" (for example abbreviations, aboutthissite etc etc).

The rest of the static file loaded with the usual "yield" in the layout. Works a treat. No more updating the menu bar all done automatically.

To get to the correct static file I created a route using:-

match 'static/:static_page_name'=> 'staticpages#show' (or in rails 2.x:-
map.connect 'static/:static_page_name', :controller=> "staticpages", :action=> "show"

"static_page_name" variable accepted anything after "/static/" in the url and passed it to the controller "staticpages" in which I set up a show action containing:-

def show
 @static_page_name = params[:static_page_name]
 allowed_pages = %w(abbreviations aboutthissite etc, etc,)
 if allowed_pages.include?(@static_page_name)
   render @static_page_name
 else
   redirect_to '/'                      #redirects to homepage if link does not exists
 end

end

I then only had to change the links in the website. (e.g.<%= link_to " About This Site ", '/static/aboutthissite' %>)

and viola! its all working.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜