[eluser]techgnome[/eluser]
I'm trying to build a re-usable component (for the lack of a better word since it's going to consist of models, views and controllers). Part of this functionality is that I want parts of it to be configurable. So I created a file and tossed it into the config folder. Now when I load my controller, I can also load my configuration settings and use them. That's all great and dandy, but now I find myself wanting to use these configuration settings outside the controller.
Example: One of the options is to use slugs or ids for accessing the postings. What I'm trying to develop is a news feed sort of use for my site, so I don't need/want a full blown CMS or blogging app. However, I don't want to build an If... Else... in my views each time I want to write out the anchor tag for it. What I'd ideally like is to have a function I could call, say nf_AnchorTitle($article) and have it return the a href tag with the slug/id as appropriate.
So I tossed it into a helper file... I quickly found out however, that I can't get to the config class from inside the helper, since it doesn't have the $this context. Nor am I willing to get a $CI reference each time I call the function, and including the controller as a parameter to the function seems like overkill. BLAGNOG!
I then thought about creating a library. But I've run into two problems with that.
1) I still can't get to the Library functions from inside my view, so it's useless.
2) Setting the config didn't work as described in the UG.
Quote:You can also pass parameters stored in a config file. Simply create a config file named identically to the class file name and store it in your application/config/ folder. Note that if you dynamically pass parameters as described above, the config file option will not be available.
Whaaaa? The last sentence I get. The first intrigued me. The second one I did... OK.. then what? How do I access them?
In the end I've come to the conclusion that I'm either going about this all bass-ackwards... or it's going to require some trick that I don't really want to do (like getting the CI instance) ... at this point, CI2.0 isn't an option. I'm still running PHP4... and there isn't anything I can do about it (was supposed to be done 3Q2010... it now being 4Q2010... we'll see).
I thought about looping through the data and creating a "URL" or "Link" property to the data... but that seems wrong... and not very efficient.
All I want is a way to enter news articles, and have them display on a "News" section of my site. For lack of a better comparison, a read-only blog... no comments or anything like that, a few categories, some admin control and that's about it.
Am I on a fool's errand? Have I lost my sanity? Or is there a way to make this work the way I want?
Ideally, I want my view to be this simple:
Code:
<?php
if ($articleCount != 0) {
?>
<h2><?php echo $articleCount; ?> News Items Found</h2>
<ol>
<?php
foreach($articles as $articleRow) {
?>
<li>
<h3><?php echo article_link($articleRow); ?></h3>
<p><?php echo $articleRow->articleContent; ?></p>
<p>Posted on: <?php echo formatDate($articleRow->articlePublishDate); ?> by <?php echo $articleRow->articleAuthorID ?></p>
<p>Category: <?php //echo $articleRow->articleCategory; ?></p>
</li>
<?php }
?>
</ol>
<?php } else echo 'No news at this time';?>
Where article_link and formatDate take care of determining if the link uses the article ID or slug based on what's in the config file, and format date takes care of formatting my dates, again, based on what's in the config file.
Right now, I feel like I'm going in circles and I'm beginning to wonder if it's even worth it anymore.
If I wasn't planning on posting the end result, I'd say frack it and just hard code everything the way I want it to be (and I still haven't ruled out this action) but I figured someone else might also find it useful, so I'm trying to keep re-usability in mind.
-tg