# |
May 19th 2021, 18:16 |
kevin.pfeifer |
you are inside a component, not a controller |
# |
May 19th 2021, 18:16 |
kevin.pfeifer |
use `$this->getController()->viewBuilder();` |
# |
May 19th 2021, 18:13 |
etibor |
is anyone tell my why i can not acces viebuilder in my component: Call to undefined method App\Controller\Component\CommonFunctionsComponent::viewBuilder() i have the line : use Cake\View; So i dont have any idea |
# |
May 19th 2021, 17:30 |
etibor |
thank you thats the best part of open source, working together and improve togather |
# |
May 19th 2021, 17:26 |
kevin.pfeifer |
thats what the support channel is for ;) |
# |
May 19th 2021, 17:20 |
etibor |
thank you all of your help, i may step one step forward in cake |
# |
May 19th 2021, 17:15 |
greg138 |
PEBKAC. :) |
# |
May 19th 2021, 17:15 |
etibor |
sorry, i was wrong, edit_document.ctp was actually empty so clearly shows empty the $this->element('edit_document'); |
# |
May 19th 2021, 17:14 |
khalil |
To test that just add a simple `<h1>Hello World</h1>` in your edit_document element |
# |
May 19th 2021, 17:13 |
greg138 |
Is there any chance that `edit_document` is just not generating any output? |
# |
May 19th 2021, 17:11 |
etibor |
thank you @kevin.pfeifer i tried like this: in controller: $this->viewBuilder()->setTemplate('edit_default'); in edit_default.ctp : echo $this->element('edit_document'); in Element : i have the edit_document.ctp but i got the layout and a blank page However its seems its looks for the edit_document.ctp(when renamed shows error) |
# |
May 19th 2021, 17:07 |
kevin.pfeifer |
if you have "logic" which is decided in the controller but you need the outcome from that logic inside the template then just set some variables like ```$use_element1 = true; $use_element2 = false; $list_of_entities = $this->MyModel->find(); $this->set(compact('use_element1', 'use_element2', 'list_of_entities'));``` and you can access these variables in your template like ```pr($use_element1); pr($use_element2);``` etc. |
# |
May 19th 2021, 17:05 |
greg138 |
If you have the entirety of what a template should show in an element (which needs to be an element because it's re-used in various places), then your template can just be one line, calling the element. |
# |
May 19th 2021, 17:02 |
kevin.pfeifer |
elements are meant to be (re-)used inside (multiple) template files They don't have anything to do with controller logic |
# |
May 19th 2021, 17:02 |
khalil |
@etibor that's not how elements are meant to be used |
# |
May 19th 2021, 17:00 |
etibor |
it the first time that i tried to use an element Can i tell cake in controller to use an element insted of the template ? |
# |
May 19th 2021, 16:57 |
etibor |
hello all |
# |
May 19th 2021, 16:54 |
me1367 |
:P |
# |
May 19th 2021, 16:54 |
khalil |
Life was much simpler back then hahahaha |
# |
May 19th 2021, 16:54 |
me1367 |
I never worked with CakePHP 2 so I have no clue how the ORM was for that :P |
# |
May 19th 2021, 16:54 |
khalil |
Then it's me |
# |
May 19th 2021, 16:53 |
me1367 |
dunno |
# |
May 19th 2021, 16:53 |
khalil |
Is it just me or the new ORM is pretty confusing? |
# |
May 19th 2021, 16:52 |
me1367 |
never gotten the time to do so xD |
# |
May 19th 2021, 16:52 |
me1367 |
My site is still not running CakePHP 4.x :P |
# |
May 19th 2021, 16:52 |
khalil |
(for your eyes only) I switch to Cake 4 months ago, after using Cake 2 for like 7 years :face_with_hand_over_mouth: |
# |
May 19th 2021, 16:50 |
me1367 |
I'm slowly upgrading it for CakePHP 4.x |
# |
May 19th 2021, 16:50 |
me1367 |
x) |
# |
May 19th 2021, 16:50 |
khalil |
PS: I'm checking admiral :) |
# |
May 19th 2021, 16:50 |
khalil |
Yuppp |
# |
May 19th 2021, 16:50 |
me1367 |
And that's where good old OOP got ya covered :) |
# |
May 19th 2021, 16:49 |
khalil |
That's basically why I wanted a way to have something I can access anywhere, instead of repeating the same logic in a component and a helper :P |
# |
May 19th 2021, 16:48 |
khalil |
Hahahaha Always :) |
# |
May 19th 2021, 16:48 |
me1367 |
also, keep your code DRY :P |
# |
May 19th 2021, 16:47 |
me1367 |
It's just basic OOP, generally, unless you really screw up, it's fine to do so :P Just make sure you keep to the _general_ *structure* of CakePHP and you'll be fine. |
# |
May 19th 2021, 16:46 |
khalil |
Very helpful! thanks again :)) |
# |
May 19th 2021, 16:46 |
khalil |
Perfect man I was struggling with this and I had no idea about the utility class, and I always worry about doing something that is considered against CakePHP principles |
# |
May 19th 2021, 16:45 |
me1367 |
Ah yes, I thought you wanted it the opposite way around (from USD to local currency, not local currency to USD). Got it. But yea, that's just in the `beforeSave` . You'd end up with something like this: ```public function beforeSave() { $converter = new CurrencyConverter(); $converted = $converter->convert($amount); }``` |
# |
May 19th 2021, 16:43 |
khalil |
That's why I need access there :) |
# |
May 19th 2021, 16:42 |
khalil |
Now in the view, I can have a simple helper that convert the product price (USD) to your currency (saved in cookie) - when the vendor wants to add a product, I convert the price they enter (in their current currency) to USD and save it in the db ( I should do that in a beforeSave hook on the model level) |
# |
May 19th 2021, 16:41 |
slackebot |
I use that, if not, I detect your ip from a local library, detect your location, and based on your location I know your currency, this only happens on first visit cause after that I save it in a cookie, |