How can integrate html template in codeigniter - codeigniter

I'm new to codeigniter. please tell me how can I integrate or install a html theme/template in codeigniter ? (my css folder path=news/css and application folder path=news/application where news is my main folder)
-thanks.

This is a very simple, very powerful way to do templates in codeigniter that is also very flexible.
http://news.dice.com/2013/02/18/how-to-build-a-to-do-app-with-codeigniter/
ignore the title, most of the lesson is about setting up templates in CI.
Note that i was first exposed to this method from a jeffrey way CI tutorial on net.tutsplus.com
All of them are worth checking out: http://net.tutsplus.com/sessions/codeigniter-from-scratch/
edit -- ok this is good enough addition to post. So in the tutorial, on the template.php page, you will see
$this->load->view($maincontent);
which is cool. but this is much better:
// load your header views
$templatefolder = 'beta/';
if(isset($content01))
$this->load->view($templatefolder.$content01);
if(isset($content02))
$this->load->view($templatefolder.$content02);
if(isset($content03))
$this->load->view($templatefolder.$content03);
// load your footer views
so instead of calling the view "maincontent", i've put in references to $content1, $content2, etc. Because we are doing if isset none of them are required. that way you can easily send more then one view file to the template. Or none at all if you are just showing a message, etc. Also notice that we have $templatefolder - that way you can easily reuse the template file for other site templates, even with the same content.
in your controller (similar to tutorial) it would be
$data['content01'] = 'codeigniterrawks';
$data['content02'] = 'mypetlion';
// beta template
$this->load->view( 'template_beta', $data );
note how easy it is if i want to bring in those same view files into a different template
$data['content01'] = 'codeigniterrawks';
$data['content02'] = 'mypetlion';
// alpha template
$this->load->view( 'template_alpha', $data );

I ran into this exact question about a week ago, this guide really helped me:
http://net.tutsplus.com/tutorials/php/an-introduction-to-views-templating-in-codeigniter/
To do the CSS url's, I added "uri" to my libraries in config/autoload.php (so it looks like this:
$autoload['libraries'] = array('uri', 'database');)
" type="text/css" media="screen" />
the base_url function automatically return whatever the base url of your site is, ie
http://localhost/news/
with the argument appended to the end.
The reason behind this is that if/when you ever need to migrate servers, you just change the base_url in the config file and it automatically updates across all of your templates and sources.

Try this,
I'm using this and it's very powerful.
https://github.com/philsturgeon/codeigniter-template

Related

ASP.NET Core MVC : Razor pages routing, incorrectly re-using previous route data

I'm working on a dotnet 6 mvc application using RazorPages, and I'm having a problem with strange routing behavior.
I have a RazorPage /Pages/News.cshtml
This page is accessible using the default route /news
When called without any parameters this page will display an index of news articles.
I also want this page to be able to display a specific news article, via a path like this...
/news/1234-my-news-article
To achieve this, I've added a config like so...
builder.Services.AddRazorPages(options =>
{
options.Conventions.AddPageRoute("/News", "News/{id:regex(^\\d+\\-.*)?}");
});
In my templates, I can then use links like this...
<a asp-page="/News" asp-all-route-data="#(new Dictionary<string, string> { { "id", "1234-my-news-article" } })">My News Article</a>
or
<a asp-page="/News">All Articles</a>
However, once I've navigated to a specific article, the index link doesn't render correctly, and will instead link again to the same article. It appears to be re-using the current routing parameters.
Is there some way to avoid this?
update:
I've found a work-around, if I use this tag instead...
<a asp-page="/News" asp-route-id="">All Articles</a>
then it will link correctly to "/news".
It seems a bit counter-intuitive to have to explicitly set this to blank. I would have assumed it would default to unset, unless explicitly set otherwise.
This is known as ambient route values (https://www.learnrazorpages.com/razor-pages/tag-helpers/anchor-tag-helper#ambient-route-values), where the current route values are automatically added to outbound links which are generated by the anchor tag helper if the destination page is the same as the current page. In older versions of Razor Pages, this was the default behaviour for all pages.
As you have discovered, you can override this by setting the route value to an empty string, or as suggested elsewhere, to use a plain anchor tag for your link.
asp-page tag helper will add id value to the route by default.It is by design.If you don't want to add it,you can try to use href to replace asp-page:
All Articles

CSS and image files not found after routing in ASP.NET MVC

In my ASP.NET MVC (3) application, I have setup the following route in global.asax.cs:
routes.MapRoute(
"UniqueId",
"{uniqueId}",
new { controller = "Book", action = "DownloadBook" },
new { uniqueId = "[0-9a-zA-Z]{5}" }
);
The DownloadFile action method is:
public ActionResult DownloadBook(string uniqueId)
{
string path = Server.MapPath(String.Format("~/App_Data/Books/{0}/index.htm", uniqueId));
if (System.IO.File.Exists(path))
{
return File(path, "text/html");
}
return new EmptyResult();
}
The method correctly serves the index.htm file from the subdirectory in the /App_Data/Books directory with the name that corresponds to the uniqueId that is defined in the route. However, the CSS and image files in the index.htm file cannot be found, since the browser tries to find them in the original URL location (e.g. http://localhost/3Yru3/).
Is there anything I can do about this? I am probably overlooking something?
EDIT (also see my comments in the answers to my question):
The books are stored as HTML files (and not as MVC Views, which would make referring to the CSS and images less of a problem) because:
1. They will be uploaded as such by users.
2. I want to store the index.htm file and the resources it uses in an HTML5 appcache, to be available offline.
EDIT 2 I have found a solution to my own question and would like to know what you think of it. See my own answer in the answers below.
This must be happening because you are referencing your files in a wrong way (relatively), meaning that it will be served according to the root of your current page.
Ex:
<link rel="Stylesheet" href="/Styles/site.css" />
or
<link rel="Stylesheet" href="../../Styles/site.css" />
Instead of this way, use the following sytax to link you CSS files and your images
<link rel="Stylesheet" href="<%=Url.Content("~/Styles/site.css")%>" />
This should work fine and be evaluated correctly from any page regardless of it's location.
You can do something, but it's more of a hack. Put placeholders for the css link then, when serving the file, do a replace, something like this.
public ActionResult DownloadBook(string uniqueId)
{
string path = Server.MapPath(String.Format("~/App_Data/Books/{0}/index.htm", uniqueId));
if (System.IO.File.Exists(path))
{
var file=File.ReadAllText(path);
//this needs a bit more refining, it's just a proof of concept
//you can use Razor templating, there is a library for that
file=file.Replace("{CssHref}",UrlHelper.GenerateContentUrl("~/Content/site.css",HttpContext));
return Content(file);
}
return new EmptyResult();
}
I think I may have found a solution to my problem that is quite elegant, but I would like to hear from you what you think of it. I have been looking at 'classic' URL rewriting in trying to send the browser to the right location, when it attempts to GET CSS files and image files that are referred to by the index.htm file in the App_Data subfolder (that has become a long sentence, I hope it makes sense ;-).
First I have tried to use Context.RewritePath in the BeginRequest event handler of the global.asax.cs of the app. That gave some unexpected side effects. Then I created a custom route class that covers both the original route described in my question above and URL rewriting for the CSS and images files requests. The custom route uses the fact that during those requests the Request.UrlReferrer property contains the URL of the location of the index.htm file. Since this is becoming a very long story, I refer to a blog post that I have written on this subject for the technical details. I hope this information will save others some valuable time.

Symfony 1.4 and pjax (ajax pushstate)?

symfony: http://www.symfony-project.org
pjax: https://github.com/defunkt/jquery-pjax
Hi all,
I'm trying to use pjax in symfony in order to speed up our website (we will be able to keep header and footer static most of the time, and also avoid reloading lots of css/js and other files).
I have no problem with ajax or symfony, but I want to know if there is a better way:
Is it a good idea to use postExecute to return the html code back right away without sf going to the template at all
If so, can I somehow write this only once for all modules? I imagine that I can do:
mySfActions extends sfActions
moduleActions extends mySfActions
I wonder if there is a better way?
3. Is there a way to get the current layout name (defined in the module's view.yml) within the controller/action?
Question 1: Don't use post-execute like that. If you need to return html from an ajax call in your action then your action should return like this:
return $this->renderText("<p>Your html result.</p>");
This will skip the template call.
Question 2: That is correct. You have written the best way to write a function once and have it available to all module actions.
There is nothing to do.
When calling an action via XmlHttpRequest, symfony automaticaly skip the Layout render, and only return the module render.
You need to put all your "static" assets and html in your layout and that's all.
Thank you all for helping me, all your answers were helpful and pointed me to the right direction. I wanted to vote for both answers but since I can only accept one, I accepted the very first answer.
Anyhow, here is what I did:
First, I extended the sfActions class so I don't have to do add preExecute on every module:
<?php
class mySfActions extends sfActions{
public function preExecute(){
$request = $this->getRequest();
if ($request->getParameter('_pjax')) {
$this->setLayout(false);
}
}
}
Then of course each of my module action class must extend this new class.
Inside my individual template I have something like this:
<?php if($sf_request->getParameter('_pjax')):?>
<script type="text/javascript" src="/js/question_list.js"></script>
<?php endif;?>
This currently seems to work quite well for me, I'm enjoy the incredible loading speed when pushstate is supported, and still able to fallback when it is not (on the dumb IE for example)

CodeIgniter Url Rewrite (language dependent)

So, i have my .htaccess, my controllers, everything is going fine. I added localization, so now i have Portuguese(Default), English and Italian.
I am using the _lang files in the appplication/languages directory, i am using session->userdata('lang') and everything works fine.
My controllers are named with portuguese words, after the top menu. What i'm looking for is:
to rewrite my url, changing the name of the controller, depending on the session->userdata('lang').
Is this even possible? how?
Thank you
So i am trying, as InFog suggested, in the routes file:
if ($this->session->userdata('lang') == 'english') {
$route['novidades/([a-z]+)'] = 'news/$1';
}
but i just get a blank screen when i open the application.
And i've tried it without the if clause, and nothing happens, when i go to
http://localhost/myapp/novidades
the url stays the same
You can solve this using CodeIgniter Routes. You can do it editing the file 'system/application/config/routes.php:
$route['news/([a-z]+)'] = 'noticias/$1';
This way an URL like '/news/run-fools' will be remaped to 'noticias/run-fools'. Now you can have just one controller =)
Good Luck
Override CI_Router to translate the name in the fetch_class() method to change controllers. Override fetch_method() to change methods.

Joomla: How to change template on a specific article

Is there a way to change the template on a specific article only?
Note that it should work without linking the article to any menu.
If you want the template override not to depend on the menu position than the standard joomla way of assigning a different template to a menu will not work. You will need to get your hands dirty and write some custom code. You will need to use the article_id as a trigger for template switch.
I did something like that at work but don't remember now how exactly this is achieved. I will post my code here as soon as I locate it.
EDIT: Found the code :)
You need to edit the file /includes/application.php, specifically the getTemplate() method. At the end of this method, just before:
// Fallback template
if (!file_exists(JPATH_THEMES.DS.$template.DS.'index.php')) {
$template = 'rhuk_milkyway';
}
you can add your condition for applying a custom template, like so:
//CUSTOM TEMPLATE FOR THE ARTICLE 13
if (JRequest::getVar('id')=='13' && JRequest::getVar('option')=='com_content') {
$template = $custom_template_name;
}
This will apply the custom template which name is inside the $custom_template_name to article with id=13. You can also use it to apply a different template to components, like I did with simplecaddy:
//TEMPLATE FOR SIMPLECADDY
if (JRequest::getVar('option')=='com_caddy'){
$template = 'shop';
}
You should really try to stay away from hard coding anything in to the template if it can be avoided. Not sure why you would specify that the article not be linked from a menu. The easiest way to accomplish this without having to write and code is to create a new menu, then add a menu item that links to the article you want to specify the template for. You don't have to put the menu in a module anywhere so it will never show up on the site, but it will show up in the menu assignment in the template manager.
You can do this with single articles, categories, sections, or even components. As long as you have a menu link to associate the template to. I always create an Admin only menu to put links that are needed to run the site, but do not need to be accessed by users.
As Brent said, avoid the temptation to modify core Joomla code! Doing this will likely stop you from doing Joomla upgrades 'cos you know it's going to break the core changes that you made.
Apart from the "hidden menu item" technique (which is useful but can break SEF URLs in some situations), a useful tool is Chameleon. This allows you to select specific articles/categories/sections (plus things like browser type, user group, component, whatever) and use these to trigger a certain template.
Though this is an old post, I thought I'd share my thoughts: You can easily change template on a single article, by implementing the onAfterInitialize() - function in a system plugin. No need to modify the Joomla core.
This works for Joomla 1.5, but should also work in 2.5:
function onAfterInitialise(){
if(true){ // f.ex. test for article ID or whatever
JRequest::setVar('template', 'beez'); // change template
}
}
In joomla 3.x versions, url-parameters are handled differently. The following was tested in joomla 3.4.8:
public function onAfterInitialise()
{
$app=JFactory::getApplication();
if(true){ // f.ex. test for article ID or whatever
$app->input->set('template', 'beez3');
}
}
More on writing plugins for Joomla here

Resources