Jump to content

Advice on chunking and function files


patrickvibes
 Share

Recommended Posts

Hi Folks,

Only a week into PW and really enjoying it, I remember viewing it >18 months ago and just moving away and sticking with EE but I can see how PW offers some great flexibility and rewarding coding if you take some time to appreciate it, especially the API.

So Ryan, awesome job! 

Now I consider myself a middle / lightweight PHP man, I understand the objective and what needs to be achieved but lack the fluency sometimes and have just spent 30 minutes threatening my laptop for a non object error - having stepped away and come back the issue was clear as day (i missed (wire) in a function file).

My question is, I have a site that has "articles" and the home page will display "sticky" or "features" articles for which I have a working start / end date date time. 

What is the best method / approach to creating "functions" includes, at the moment I'm working on the homepage and thinking I create a home_functions.inc and that contains all homepage functions to load featured news, featured gallery items etc. Is this ok, or am I better dealing with each "component" of the site with it's own functions file and calling them as required, i.e.

gallery_functions.inc

- handles gallery categories

- handles single ablum

- handles single image

news_functions.inc

- handles cateogries

- latest news

- single news post

My Ee workflow would be something like:

- Create basic template like news.php

- Create a single news loop that I can pass variables into to use for archive, categories based on URL segment variables

I'd appreciated any tips as the whole PW experience is great and I want to start on the best footing.

Link to comment
Share on other sites

Hi Patrickvibes,

Welcome to PW and the forums :-)...In a hurry so I'll just point you to this nice tutorial and this discussion for now...other's will chime in:

http://processwire.com/docs/tutorials/how-to-structure-your-template-files/

https://processwire.com/talk/topic/740-a-different-way-of-using-templates-delegate-approach/ - might seem overwhelming if you are still new to PHP though.... :-)

Edit:

Forgot...there is no best method really :-)

Edited by kongondo
Link to comment
Share on other sites

Hi Kongondo

Thanks for the fast reply, I notice a nice buzz in these forums which is always so good to see on OS projects.

I've had power read through them, I suppose my real question is if I create a function file for every "component" of the site i.e. new, events, gallery etc - am I going to cause significant overhead by loading "gallery_fucntions.inc" on the homepage just to output one featured image from an album, likewise with news. Or - is it better to create a home_functions to load only parts of the homepage but in doing so I have to repeat code through function files like:

in my news_functions:

Show a sticky post

Show all posts

Show post by category

Show posts by archives

so to load that function file in my homepage am I processing everything or only what I call from myfucntion() like getSticky() ?

I suppose what I want to try and get approval on is whether it's good practice to have functions files handle all my queries for loops etc and call them in templates so as to separate logic from templates ish.


P.S. It's nice to also see a very friendly forum with people who politely offer guidance, there are plenty of projects out there with grumpy vets who leap at the chance to flame a new user.

Link to comment
Share on other sites

P.P.S. I know my way round PHP enough to have hashed together something fairly tangible in PW since trying it, I've a pretty long experience wrestling WordPress, long time with EE and build a lot of E-commerce sites that said, my background is not Comp Sc, I've learned PHP over the years through trial and error like "is my brother dumber than a hamster" "Ouch" ;)

Link to comment
Share on other sites

hi patrick

i can't really help you with your question about what is better, but you can have a look how ryan does it ;)

in the blog profile he has one .inc file with some functions (blog.inc): https://github.com/ryancramerdesign/BlogProfile/tree/master/templates

in the dev branch he uses a _func.php file: https://github.com/ryancramerdesign/ProcessWire/tree/dev/site-default/templates

maybe you could also split it up to one file with shared functions and seperate files with only the functions related to your "components"

i don't know about performance issues though, but i think seperating code from design should be a benefit compared to loading some more bytes of unused functions... maybe you will get more qualified answeres on that point :)

Link to comment
Share on other sites

Thanks BernhadB

Appreciated the pointer, I know everyone has their own preference, suppose I;m just interested in different peoples preference and approach and I know there are many ways to skin a cat :)

I'm really enjoying PW so far and realising that it's a great CMF to get people learning and thinking about projects, the ease of defining a query is truly awesome I mean:

$mything = wire("pages")->find("template=my_template, my_field=special, my_field_featured_start<=today, my_field_featured_end>=today, limit=4, sort=random");

is just so simply to grasp and execute, I love the ability to tap into any field you define.

Give me another week or so and I'll hopefully be posting my approaches and opening them up to either gratitude or constructive criticism, I'm looking forward to participating in this forum :)


P.S. Even over the last couple of hours I've realised my frustration last night looking at the simple blog tutorial I was able to deviate from it all together and hash some functions together, PW makes PHP fun!!!

  • Like 4
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...