Giter Club home page Giter Club logo

ci-smarty-jquery-to-ci-angular-guide's Introduction

from [CI, Smarty, jQuery] to [CI, Angular] Guide

##First Step: Remove Smarty

###Controller

####1. Parse Tpl -

[CI, Smarty, jQuery] way

$this->load->library('parser');
$this->parser->parse('dirPath/file.tpl', $data);

[CI, Angular] way

$this->load->view('dirPath/file.php', $data);

###View

####1. File Ext -

file.tpl -> file.php

####2. Include File -

[CI, Smarty, jQuery] way

{include file="dirPath/file.tpl"}

[CI, Angular] way

<?php include APPPATH.'views/dirPath/file.php'; ?>

####3. Include Var -

[CI, Smarty, jQuery] way

{$var}

[CI, Angular] way

<?=$var?>

####4. If / else - [CI, Smarty, jQuery] way

{if codition}
    <input name="test" value="true">
{else}
    <input name="test" value="false">
{/if}

[CI, Angular] way

<?php if (condition) { ?>
    <input name="test" value="true">
<?php } else { ?>
    <input name="test" value="false">
<?php } ?>

####5. foreach - [CI, Smarty, jQuery] way

{foreach key=i from=$data_list item=data}
    <li data-key="{$i}">{$data}</li>
{/foreach}

[CI, Angular] way

<?php foreach ($data_list as $i => $data) { ?>
    <li data-key="<?=$i?>"><?=$data?></li>
<?php } ?>

####6. time - [CI, Smarty, jQuery] way

<?=$time|date_format:'%d'?>

[CI, Angular] way

<?=strftime('%d', $time)?>

##Second Step: Start Angular

####1. Add Script need add others for another usages(at html head)

<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular-route.min.js"></script>

js for this page(at html body bottom)

<script src="dirPath/js/thisPage.js"></script>

####2. Add App Scope (at body)

<body ng-app="app">

####3. Do thisPage.js Init

var app = angular.module('app', ['ngRoute']);

####4. Page Route body div for view

<div ng-view>

use ng-template (at html body bottom) part separate to individual php file

    <script id="part1.html" type="text/ng-template">
        <?php include APPPATH.'views/filePath/firstPart.php'; ?>
    </script>
    <script id="part2.html" type="text/ng-template">
        <?php include APPPATH.'views/filePath/secondPart.php'; ?>
    </script>
    <script id="part3.html" type="text/ng-template">
        <?php include APPPATH.'views/filePath/thirdPart.php'; ?>
    </script>

thisPage.js add route config

    app.config(function($routeProvider) {
        $routeProvider
            .when('/part1', {
                controller:'FirstPartControl',
                templateUrl:'part1.html'
            })
            .when('/part2', {
            controller:'SecondPartControl',
            templateUrl:'part2.html'
            })
            .when('/part3', {
                controller:'ThirdPartControl',
                templateUrl:'part3.html'
            })
            .otherwise({
                redirectTo:'/part1'
            });
    });

####5. Add Controller

    app.controller('FirstPartControl', ['$scope', function($scope) {
        $scope.datas = [{key: value1}, {key: value2}];
        $scope.addData = function(data) {
            $scope.datas.push(data);    
        }
    }]);

####6. Redirect In Controller function (add param $location) [CI, Smarty, jQuery] way

    function redirect() {
        location.href = 'path/part1';
    }

[CI, Angular] way

    app.controller('SecondPartControl', ['$scope', '$location', function($scope, $location) {
        $scope.toPart1 = function() {
            $location.path('/part1');
        };
    }]);

####7. Ajax In Controller function (add param $http) [CI, Smarty, jQuery] way

    $.ajax({
        type: 'POST',
        url: 'some.php',
        data: { "foo": "bar" }
    }).done(function(data) {
        // get data to view
    }).fail(function(jqXHR, textStatus) {
        // error msg
    });

[CI, Angular] way

    app.controller('thirdPartControl1', ['$scope', '$http', function($scope, $http) {
        $scope.ajax = function() {
            $http({
                url: 'http://www.example.com/targetUrl',
                method: 'POST',
                data: { "foo": "bar" }
            }).success(function(data, status, headers, config) {
                $scope.data = data;
            }).error(function(data, status, headers, config) {
                $scope.status = status;
            });
        };
    }]);

###8. Share Data between Controllers factory data value for share getValue setValue method

    app.factory('data', [function () {
        var dataValue = '',
            Data = {
                getDataValue: function() {
                    return this.dataValue;
                },
                setDataValue: function(dataValue) {
                    this.dataValue = dataValue;
                }
            };
        return Data;
    }]);

controller1

    app.controller('FirstPartControl', 'data', ['$scope', function($scope, $data) {
        $scope.datas = $data.getDataValue();
    }]);

controller2

    app.controller('secondPartControl', 'data', ['$scope', function($scope, $data) {
        $scope.doSomeThing = function() {
            $data.setDataValue($scope.data);
        };
    }]);

####9. Foreach [CI, Angular] way (pervious)

<?php foreach ($data_list as $i => $data) { ?>
    <li data-key="<?=$i?>"><?=$data?></li>
<?php } ?>

[CI, Angular] way

<li data-key="{{$index + 1}}" ng-repeat="data in data_list"></li>

####10. Click [CI, Smarty, jQuery] way

<button type="submit" id="want-to-click"></button>
    $('#want-to-click').on('click', function() {
        doSomething();
    });

[CI, Angular] way

<button type="submit" ng-click="deleteData()"></button>
    app.controller('Controller', ['$scope', function($scope) {
        $scope.datas = [{key: value1}, {key: value2}];
        $scope.deleteData = function() {
            $scope.datas.pop();    
        }
    }]);

####11. Show & hide [CI, Smarty, jQuery] way

<div id="want-to-show"><div>
<div id="want-to-hide"><div>
$('#want-to-show').show();
$('#want-to-hide').hide();

[CI, Angular] way

<div ng-show=""><div>
<div ng-hide=""><div>

####12. Data Init [CI, Angular] way

controller php

$data['for_init'] = json_encode($for_init)

view php

<div ng-init="data = <?=$for_init?>">

js

    app.controller('Controller', ['$scope', function($scope) {
        var for_init = JSON.parse($scope.data);
        ........
        }
    }]);

ci-smarty-jquery-to-ci-angular-guide's People

Contributors

chentsulin avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.