Controller and functions

There is one thing I can’t understand about controllers.

I’m ok with the concept to separate logic from layout, but what if I need to put several functions inside the controller, without having the code executed from the top to the bottom like in Procedural programming?

The example from Kirby site is pretty simple; it’s what is inside the loop (following the example) that is plenty of code dealing with the business logic of the page.

For example, here is how I need to get the author avatar / name:

//Autore con link
		$loop_writerLink = '<a onclick="javascript:hnt_showArticleList(996,1,\''.$article->writer().'\');">' . $u_name . '</a>';

		//Avatar, se presente
		$loop_writerImgLink = '';
		if ($u_avatarRoot > '') 
		{
			$loop_writerImgLink = '<a onclick="javascript:hnt_showArticleList(996,1,\''.$article->writer().'\');">' 
							. '<img src="' . $u_avatarRoot . '"' 
							. 'class="img-circle" style="width: 28px; height: 28px;">' 
							. '</a>';
		}

I would like to have a function getAuthorLinks() inside the controller and just a simple way to have the returned value to the template file.

Thanks

Unfortunately, I don’t understand what you are trying to achieve here. What are all these variables? Why are you mixing html with variables to define new variables?

i do the following setup:

  • site.php: generic helper functions and includes from composer to custom site.php
  • controllers: to prepare data for template, check authentification or code an json-api (could be done using routes and a plugin as well)
  • models: to extend templates with additional properties like getting a custom image link based on value of field of a template
  • templates: to iterate over prepared data and use functions defined in model to build html code
  • snippets: to create code i reuse in diffentent templates. you can forward variables to snippets and very useful as well get the value returned and not echoed (third param set to true)

so based on your example ‘creating a link from template’ i would use a model function or a snippet.

2 Likes

@texnixe yes probably my example is not the neatest I could provide, but @bnomei really helped me to better understand some concepts. I wasn’t aware of models and probably models are what I need. I’m still a Kirby 1.0 guy :slight_smile: